为什么我不能将背景图像设置到从相关片段 class 声明到片段 xml 配置中的 ImageView?

Why I can't set a background image into an ImageView declared into a fragment xml configuration from the related fragment class?

我绝对是 Android 开发新手,我在开发我的第一个应用程序时遇到了一些问题。

所以我有以下情况:

1) 我有这个 fragment_screen_slide_page.xml 文件,其中包含显示在视图中的片段元素,并且在加载图像时必须显示图像一个 activity:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Dummy content. -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:scaleType="fitCenter"
            android:layout_height="250dp"
            android:background="@drawable/carbonara" />

        <TextView android:id="@android:id/text1"
            style="?android:textAppearanceLarge"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />

    </LinearLayout>

</ScrollView>

如您所见,它包含具有 id=imageView1ImageView 元素(当片段是放入 activity).

然后我有这个 ScreenSlidePageFragment 适用于前一个片段,我有这样的东西:

public class ScreenSlidePageFragment 扩展片段 {

................................................................
................................................................
................................................................

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

   System.out.println("PAGE NUMBER: "+ mPageNumber + 1);
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);

    // Set the title view to show the page number.
    ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.string.title_template_step, mPageNumber + 1));
    System.out.println("TESTO: "+ getString(R.string.title_template_step, mPageNumber + 1));

    // Obtain the colosseum_icon.png from the rsources as a Drawable:
    Drawable drawable = getResources().getDrawable(R.drawable.colosseum_icon);

    // Retrieve the reference of the ImageView element of the layout having id=imgSlide:
    View view = inflater.inflate(R.layout.fragment_screen_slide_page,container,false);
    ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);

    final int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        imgSlideView.setBackgroundDrawable(drawable);
    } else {
                              //imgSlideView.setBackground(getResources().getDrawable(R.drawable.colosseum_icon));
    }

    return rootView;
}
    ................................................................
    ................................................................
    ................................................................
}

................................................................
................................................................
................................................................

}

所以大家可以看到,进入onCreateView()方法我基本上做了如下操作:

1) 我检索了 ImageView 元素引用,其中在之前的 [=65= 中声明了 id=imageView1 ] 文件.

2) 我将检索到的可绘制对象设置为此 ImageView 的背景,执行以下操作:

imgSlideView.setBackgroundDrawable(drawable)

我希望这张图片必须显示在我的屏幕上,但它并没有出现。未显示与检索到的 drawable 变量相关的图像。而不是图像出现一个空 space,高度为 250dp(如 XML 中指定)。在空图像下出现 TextView

的预期内容

为什么?我错过了什么?我该如何解决这个问题?

Tnx

首先,您希望在 ImageView 上使用 android:src="@drawable/carbonara" 而不是 android:background="@drawable/carbonara",并且设置 .setImageDrawable 而不是 .setBackgroundDrawable

下一个 - View view = inflater.inflate(R.layout.fragment_screen_slide_page,container,false); 不需要,应将其删除。而不是 ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1); 应该是 ImageView imgSlideView = (ImageView) rootView.findViewById(R.id.imageView1);

当然还有这个

final int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
        imgSlideView.setBackgroundDrawable(drawable);
}

仅在 sdk 低于 Jelly Bean 时激活(我不确定这是否适用于您的测试环境)。

函数inflate每次都会生成一个新视图。你调用了两次,所以你有两个单独的视图:view 和 rootView,以及 returnrootView,所以片段将显示 rootView,没有视图。没有图像。像这样的变化:

    // Retrieve the reference of the ImageView element of the layout having id=imgSlide:
ImageView imgSlideView = (ImageView) rootView.findViewById(R.id.imageView1)

年,以及更多,ImageView应该使用android:src和setImageDrawable

一切都会好起来的。

已添加,此代码块有问题:

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
       // if your phone version below 4.1
    imgSlideView.setBackgroundDrawable(drawable);
} else {
                          //imgSlideView.setBackground(getResources().getDrawable(R.drawable.colosseum_icon));
}

如果您的 phone、android 版本大于 4.1(我认为是 -_-|),则不要更改图像。所以取消注释其他代码。

使用 setImageResource() 而不是 setBackgroundDrawable()

所以应该是

imageView.setImageResource(R.drawable.your_image);