I have a linearLayout as the main layout in my android x开发者_如何学运维ml file. The background is set to "@drawable/window_background_red". Inside the activity (at run time) I want to set this to "@drawable/window_background_green".
window_background_green.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:type="linear"
android:startColor="#000000"
android:endColor="#004400"
android:angle="-90"
/>
</shape>
</item>
</selector>
let's say that for linearLayout android:id="@+id/linearLayout"
then in code:
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
linearLayout.setBackgroundResource(R.drawable.window_background_green);
And there, you've set the background to be @drawable/window_background_green
精彩评论