ImageView In Fragment 未显示

Joe*_*cer 1 java android android-layout android-imageview android-fragments

我有一个奇怪的问题,我有一个解决方法,但想知道是否有人可以解释为什么会发生这种情况以及更优雅的解决方案?

我有一个带有一系列片段的 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;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.wizard_fragment_step_1, container, false);
        ImageView remoteImage9Button = (ImageView)v.findViewById(R.id.remote9ButtonImage);
        remoteImage9Button.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.flamerite_remote_9, null)); //THIS IS THE LINE THAT MAKES THE IMAGE WORK AGAIN
        return v;
    }
}
Run Code Online (Sandbox Code Playgroud)

我当然可以保留对 imageDrawable 的重新设置,但认为仅在 xml 布局中设置时图像不应该显示很奇怪

任何想法都非常感谢!!

Vin*_*ino 15

我不知道原因,但在我的情况下,当我改变时同样的问题得到了解决

图像 src 定义来自

app:srcCompat="@drawable/logo"
Run Code Online (Sandbox Code Playgroud)

android:src="@drawable/logo"
Run Code Online (Sandbox Code Playgroud)