Android的设置的LinearLayout后台编程 [英] Android setting LinearLayout background programmatically

查看:199
本文介绍了Android的设置的LinearLayout后台编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要在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(四);
 

但它崩溃。任何想法?

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

  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.

这篇关于Android的设置的LinearLayout后台编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆