Android 以编程方式设置 LinearLayout 背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12781501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:04:42  来源:igfitidea点击:

Android setting LinearLayout background programmatically

androidlayout

提问by Buffalo

I have a situation where I need to set a background on a LinearLayout programatically.

我有一种情况,我需要以编程方式在 LinearLayout 上设置背景。

In my layout, I am setting my background using `android:background="?android:attr/activatedBackgroundIndicator", but I want to set this programatically:

在我的布局中,我使用 `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" >

I've tried using:

我试过使用:

Drawable d = getActivity().getResources().getDrawable(android.R.attr.activatedBackgroundIndicator);
rootLayout.setBackgroundDrawable(d);

But it crashes. Any ideas?

但它崩溃了。有任何想法吗?

Edit: I had also tried using:

编辑:我也试过使用:

rootLayout.setBackgroundResource(android.R.attr.activatedBackgroundIndicator);

10-08 15:23:19.018: E/AndroidRuntime(11133): android.content.res.Resources$NotFoundException: Resource ID #0x10102fd

回答by mlabraca

I had the same problem and I fixed it using this piece of code.

我遇到了同样的问题,我使用这段代码修复了它。

The android.R.attr.* are pointers to the in a theme and not to the actual drawable resource defined. You have to use the TypedArrayto access the id.

android.R.attr.* 是指向主题中的指针,而不是指向定义的实际可绘制资源的指针。您必须使用TypedArray来访问 ID。

theView = this.inflater.inflate(R.layout.list_row_job_favorited, null);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
  TypedArray a = mContext.obtainStyledAttributes(new int[] { android.R.attr.activatedBackgroundIndicator });
  int resource = a.getResourceId(0, 0);
  //first 0 is the index in the array, second is the   default value
  a.recycle();

  theView.setBackground(mContext.getResources().getDrawable(resource));
}

I used this in my custom list adapter when detects SDK upper and worked fine.

当检测到 SDK upper 并且工作正常时,我在我的自定义列表适配器中使用了它。

回答by Syn3sthete

try this line

试试这条线

rootLayout.setBackgroundResource(d);

instead of

代替

rootLayout.setBackgroundDrawable(d);

回答by Naveen Kumar

try this

尝试这个

rootLayout.setBackgroundResource(R.drawable.image);

回答by Yonatan Nir

It's a bad idea doing it the way the accepted answer tells you to. The problem is that you also need to call the list's onItemCheckedStateChangedto update what's needed (the action bar title for example).

按照公认的答案告诉你的方式去做是一个坏主意。问题是您还需要调用列表onItemCheckedStateChanged来更新所需的内容(例如操作栏标题)。

In that case all you need to do is simply call getListView().setItemChecked(position, true);when the item is checked and getListView().setItemChecked(position, false);when it's not checked.

在这种情况下,您需要做的只是getListView().setItemChecked(position, true);在检查项目和getListView().setItemChecked(position, false);未检查项目时调用。

回答by Farhan Khan

Please try the following code.

请尝试以下代码。

LinearLayout layout=(LinearLayout) findViewById(R.id.layoutImage);
layout.setBackgroundResource(R.drawable.bg);

回答by Amilcar Andrade

You can use something like this

你可以使用这样的东西

TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroun d, outValue, true); view.setBackgroundResource(outValue.resourceId);

TypedValue outValue = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroun d, outValue, true); view.setBackgroundResource(outValue.resourceId);