Android的设置的LinearLayout后台编程后台、Android、LinearLayout

由网友(始于心动终于白首)分享简介:我有一个情况我需要在LinearLayout中设置背景编程。 在我的布局,我设置我的背景使用`机器人:背景=机器人:ATTR / activatedBackgroundIndicator,但我想设置这个编程:< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.co...

我有一个情况我需要在LinearLayout中设置背景编程。

在我的布局,我设置我的背景使用`机器人:背景=机器人:ATTR / activatedBackgroundIndicator,但我想设置这个编程:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / myLayoutId
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =FILL_PARENT
    机器人:重力=左|中心
    机器人:paddingBottom会=5dip
    机器人:以下属性来=5dip
    机器人:背景=机器人:ATTR / activatedBackgroundIndicator
    机器人:paddingTop =5dip>
 

我使用的尝试:

 绘制对象D = getActivity()getResources()getDrawable(android.R.attr.activatedBackgroundIndicator)。
rootLayout.setBackgroundDrawable(四);
 
Android绝对布局

但它崩溃。任何想法?

编辑:我一直在使用也尝试过:

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

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

解决方案

我有同样的问题,我固定它使用这块code。

在android.R.attr。*是指向了一个主题,而不是定义的实际绘制资源。你必须使用 TypedArray 的访问ID。

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

如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_ codeS.HONEYCOMB){
  TypedArray A = mContext.obtainStyledAttributes(新INT [] {android.R.attr.activatedBackgroundIndicator});
  INT资源= a.getResourceId(0,0);
  //第一个0是该指数在数组中,第二个是默认值
  a.recycle();

  theView.setBackground(mContext.getResources()getDrawable(资源)。);
}
 

我曾经在我的自定义列表适配器时检测SDK上正常工作。

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

解决方案

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.

阅读全文

相关推荐

最新文章