我有一个奇怪的问题,我有一个解决方法,但想知道是否有人可以解释为什么会发生这种情况以及更优雅的解决方案?
我有一个带有一系列片段的 ViewPager。Fragment 使用自己的布局,当 ImageView 添加到布局时,ImageView 不会在运行时呈现。
如果我在 Fragment 的 onCreateView 方法中为 ImageView 显式设置 imageDrawable(即使图像与布局 xml 中引用的图像相同,则图像显示正常。
这是我的片段 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/baseRelLayout"
android:background="@color/colorPrimary">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/flamerite_remote_6"
android:layout_marginStart="36dp"
android:id="@+id/remote6ButtonImage"
android:layout_alignParentBottom="false"
android:layout_alignStart="@+id/textView3"
android:scaleType="fitXY"
android:background="@color/colorPrimary"
android:layout_alignBottom="@+id/remote9ButtonImage" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是我在 onCreateView 中重新设置可绘制图像的地方
public class WizardFragment extends Fragment {
public static final String STEP_NUMBER = "STEP_NUMBER";
public static final WizardFragment newInstance(String message){
WizardFragment f = new WizardFragment();
Bundle bdl = new Bundle(1);
bdl.putString(STEP_NUMBER,message);
f.setArguments(bdl);
return f;
} …Run Code Online (Sandbox Code Playgroud) java android android-layout android-imageview android-fragments