我绝对是Android开发的新手,在开发第一个应用程序时遇到了一些问题。
所以我有以下情况:
1)我有这个fragment_screen_slide_page.xml文件,其中包含显示在视图中的片段元素,并且在将其加载到活动中时必须显示图像:
<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 = imageView1的ImageView元素(将片段放入活动时必须在其中加载图像)。
然后,我有了这个ScreenSlidePageFragment,它可以在先前的片段上工作,我有这样的东西:
公共类ScreenSlidePageFragment扩展Fragment {
................................................................
................................................................
................................................................
@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)我检索了在以前的fragment_screen_slide_page.xml文件中声明的id = imageView1的ImageView元素引用。
2)我设置一个检索到的drawable作为此ImageView的背景:
imgSlideView.setBackgroundDrawable(drawable)
我希望此图像必须显示在我的屏幕上,但不会发生。未显示与检索到的drawable变量相关的图像。代替图像出现的是一个高度为250dp的空白区域(已在XML中指定)。在空白图片下显示TextView的预期内容
为什么?我想念什么?如何解决此问题?
特纳克斯
最佳答案
首先,您要在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(我不确定这是否适用于您的测试环境)。
关于java - 为什么我不能将背景图像设置到相关 fragment 类中声明为 fragment xml配置的ImageView中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37926153/