片段中的ImageView不显示

ImageView In Fragment not showing

我有一个奇怪的问题,我有一个解决方法,但想知道是否有人可以阐明为什么会发生这种情况,并且可能有更优雅的修复方法?

我有一个包含一系列片段的 ViewPager。 Fragments 使用自己的布局,当 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>

这是我在 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;
    }
}

我当然可以保留 imageDrawable 的重新设置,但我觉得奇怪的是,当仅在 xml 布局

中设置时,图像不应该显示

非常感谢任何想法!!

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

来自

的图像 src 定义
app:srcCompat="@drawable/logo"

android:src="@drawable/logo"

只需添加 android:src="@drawable/logo" 而不是 app:srcCompat