从code更改活动背景背景、code

2023-09-12 05:16:17 作者:你不会遇见第二个我

我有一个背景的活动:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="@drawable/background_fingerboard" />

如何更改背景图片从code?实际上我用单声道,而且Java的样品也将是有帮助的。

How can I change background image from code? I actually use mono, but Java samples will also be helpful.

推荐答案

先添加的LinearLayout ID作为在布局的xml:

first add LinearLayout id as in your layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayoutid" 
android:minWidth="25px"
android:minHeight="25px"
android:background="@drawable/background_fingerboard" 

在code部分集背景::

and in code part set background as::

LinearLayout  linearLayout = (LinearLayout) findViewById(R.id.linearLayoutid);
 linearLayout.setBackgroundResource(R.drawable.background_fingerboard);