Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 设置背景图像_Java_Android_Background - Fatal编程技术网

Java 设置背景图像

Java 设置背景图像,java,android,background,Java,Android,Background,我已通过以下方式设置了我活动的背景: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/image"> 我在做什么:在我的主要活动中,它将选择我是想要image1还是image2,

我已通过以下方式设置了我活动的背景:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="@drawable/image">

我在做什么:在我的主要活动中,它将选择我是想要image1还是image2,然后将image1或image2作为活动的背景


谢谢

使用
LinearLayout
方法
setBackgroundResource
并为其提供可绘制id。

使用
LinearLayout
方法
setBackgroundResource
并为其提供可绘制id。

试试这个

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear);
if(condition1)
linearLayout.setBackgroundResource(R.drawable.image1);
else
linearLayout.setBackgroundResource(R.drawable.image2);
在XML中

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/linear"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
/>

这会奏效的。根据条件,它将设置image1或image2

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear);
if(condition1)
linearLayout.setBackgroundResource(R.drawable.image1);
else
linearLayout.setBackgroundResource(R.drawable.image2);
在XML中

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:id = "@+id/linear"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
/>


这会奏效的。根据条件,它将设置image1或image2

您尝试了什么?您将从何处选择图像?在下一个活动中显示该图像?您尝试了什么?您将从何处选择图像?在下一个活动中显示该图像?:)用户1330394:如果有效,则将其标记为正确答案:)用户1330394:如果有效,则将其标记为正确答案