java - fragment 中的 ImageView 未显示

标签 java android android-layout android-fragments android-imageview

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

我有一个带有一系列 fragment 的 ViewPager。 Fragment 使用自己的布局,当将 ImageView 添加到布局时,ImageView 不会在运行时呈现。

如果我在 Fragment 的 onCreateView 方法中显式设置 ImageView 的 imageDrawable (即使该图像与布局 xml 中引用的图像相同,则图像显示正常。

这是我的 Fragment 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"

关于java - fragment 中的 ImageView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42859402/

相关文章:

java - 持久化对象时出现 "org.apache.openjpa.persistence.PersistenceException: null keys not allowed"错误。

java - 需要帮助来运行我的 swing 图形代码

java - Java 中二维 int 数组优化的洪水填充

java - 在回收器 View 中的嵌套回收器 View 内的嵌套列表中添加项目

android-layout - 在运行时获取布局的高度和宽度

java - log4j - RollingFileAppender - 日期模式和 MaxFileSize

android - 取消选中布局中 android 中所有动态创建的复选框

java - JNI C函数无法解析android串口Api

android - fragment 显示正在重置

Android 文本在固定高度 TextView 上被截断