【发布时间】:2012-10-08 12:11:49
【问题描述】:
我遇到需要以编程方式在 LinearLayout 上设置背景的情况。
在我的布局中,我使用 `android:background="?android:attr/activatedBackgroundIndicator" 设置背景,但我想以编程方式设置:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myLayoutId"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:background="?android:attr/activatedBackgroundIndicator"
android:paddingTop="5dip" >
我尝试过使用:
Drawable d = getActivity().getResources().getDrawable(android.R.attr.activatedBackgroundIndicator);
rootLayout.setBackgroundDrawable(d);
但它崩溃了。有什么想法吗?
编辑:我也尝试过使用:
rootLayout.setBackgroundResource(android.R.attr.activatedBackgroundIndicator);
10-08 15:23:19.018: E/AndroidRuntime(11133): android.content.res.Resources$NotFoundException: Resource ID #0x10102fd
【问题讨论】:
-
发布 logcat 有助于找到崩溃的解决方案
-
下次添加logcat。我认为您的 minSdk 不是 11,并且您在 api
-
您使用的是哪个 API 级别?
-
这意味着你试图找到的资源没有找到,如果你想得到一个 dRawable ,你应该从 R.drawable (或 android.R.drawable )而不是从 android 中获取它。 R.attr
-
我知道 activateBackgroundIndicator 在 11 之前不存在,我检查了一下,这不是问题。