Android setting LinearLayout background programma

2019-03-15 23:06发布

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

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

<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

6条回答
【Aperson】
2楼-- · 2019-03-15 23:09

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 TypedArray to access the 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.

查看更多
看我几分像从前
3楼-- · 2019-03-15 23:09

try this line

rootLayout.setBackgroundResource(d);

instead of

rootLayout.setBackgroundDrawable(d);
查看更多
叼着烟拽天下
4楼-- · 2019-03-15 23:20

Please try the following code.

LinearLayout layout=(LinearLayout) findViewById(R.id.layoutImage);
layout.setBackgroundResource(R.drawable.bg);
查看更多
smile是对你的礼貌
5楼-- · 2019-03-15 23:25

try this

rootLayout.setBackgroundResource(R.drawable.image);
查看更多
混吃等死
6楼-- · 2019-03-15 23:25

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 onItemCheckedStateChanged to update what's needed (the action bar title for example).

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.

查看更多
戒情不戒烟
7楼-- · 2019-03-15 23:26

You can use something like this

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

查看更多
登录 后发表回答