[英]Why I can't set a background image into an ImageView declared into a fragment xml configuration from the related fragment class?
I am absolutely new in Android development and I have some problem developing my first app.我是 Android 开发的新手,我在开发我的第一个应用程序时遇到了一些问题。
So I have the following situation:所以我有以下情况:
I have this fragment_screen_slide_page.xml file that contains the element of a fragment that is shown into a view and have to show an image when it is loaded into an activity:我有这个fragment_screen_slide_page.xml文件,其中包含显示到视图中的片段元素,并且在将其加载到活动中时必须显示图像:
<!-- 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>
As you can see it contains the ImageView element having id=imageView1 (where the image have to be loaded when the fragment is putted into the activity).如您所见,它包含具有id=imageView1的ImageView元素(当将片段放入 Activity 时必须加载图像)。
Then I have this ScreenSlidePageFragment that works on the previous fragment, I have something like this:然后我有这个适用于前一个片段的ScreenSlidePageFragment ,我有这样的东西:
public class ScreenSlidePageFragment extends 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;
}
................................................................
................................................................
................................................................
}
................................................................
................................................................
................................................................
}
So as you can see, into the onCreateView() method I basically do the following operation:如您所见,进入onCreateView()方法我基本上做了以下操作:
I retrieve the ImageView element reference having id=imageView1 declared inside the previous fragment_screen_slide_page.xml file.我检索在前一个fragment_screen_slide_page.xml文件中声明的id=imageView1的ImageView元素引用。
I set a retrieved drawable as background of this ImageView doing:我将检索到的可绘制对象设置为此ImageView的背景:
imgSlideView.setBackgroundDrawable(drawable)
I expect that this image have to be shown on my screen but it don't happen.我希望这张图片必须显示在我的屏幕上,但它不会发生。 The image related to the retrieved drawable variable is not shown.
与检索到的可绘制变量相关的图像未显示。 Instead of the image appear an empty space having 250dp as height (as specified into the XML).
代替图像出现一个高度为250dp的空白空间(如 XML 中指定的那样)。 Under the empty image appear the expected content of the TextView
在空图像下出现TextView的预期内容
What am I missing?我错过了什么? How can I fix this issue?
我该如何解决这个问题?
Firstly you would want to use android:src="@drawable/carbonara"
instead of android:background="@drawable/carbonara"
on your ImageView and you set .setImageDrawable
instead of .setBackgroundDrawable
首先,您想在 ImageView 上使用
android:src="@drawable/carbonara"
而不是android:background="@drawable/carbonara"
并设置.setImageDrawable
而不是.setBackgroundDrawable
Next - View view = inflater.inflate(R.layout.fragment_screen_slide_page,container,false);
下一步 -
View view = inflater.inflate(R.layout.fragment_screen_slide_page,container,false);
is not needed and should be removed.不需要,应该删除。 Instead of
ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
而不是
ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
it should be ImageView imgSlideView = (ImageView) rootView.findViewById(R.id.imageView1);
它应该是
ImageView imgSlideView = (ImageView) rootView.findViewById(R.id.imageView1);
Also this of course当然这也是
final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
imgSlideView.setBackgroundDrawable(drawable);
}
is only activated is sdk is lower than Jelly Bean (I'm not sure if that applies to your test environment).仅激活是 sdk 低于 Jelly Bean(我不确定这是否适用于您的测试环境)。
The function inflate will generate a new view every time.函数 inflate 每次都会生成一个新视图。 you called twice, so you have two seperate view: view and rootView , and return rootView, so The fragment will show rootView, no view.
你调用了两次,所以你有两个单独的视图: view 和 rootView ,并返回 rootView,所以片段将显示 rootView,没有视图。 no image.
没有图像。 changes like this:
像这样的变化:
// Retrieve the reference of the ImageView element of the layout having id=imgSlide:
ImageView imgSlideView = (ImageView) rootView.findViewById(R.id.imageView1)
Year, And more, ImageView should use android:src and setImageDrawable年多,ImageView 应该使用 android:src 和 setImageDrawable
Everything will be ok.一切都会变好。
added, this code block has problem:添加,此代码块有问题:
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));
}
if your phone, android version is bigger than 4.1 (I think so -_-|), you don't change your image.如果您的手机,android 版本大于 4.1(我认为是 -_-|),您不会更改图像。 so uncomments the else code.
所以取消注释 else 代码。
Use setImageResource() instead of setBackgroundDrawable()使用 setImageResource() 而不是 setBackgroundDrawable()
So it should be所以应该是
imageView.setImageResource(R.drawable.your_image); imageView.setImageResource(R.drawable.your_image);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.