设置背景图像

时间:2012-04-30 05:03:11

标签: 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,然后将image1或image2作为活动的背景。

由于

2 个答案:

答案 0 :(得分:3)

试试这个

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

答案 1 :(得分:0)

使用LinearLayout方法setBackgroundResource并为其提供drawble id。

相关问题