【问题标题】:Android setting LinearLayout background programmaticallyAndroid以编程方式设置LinearLayout背景
【发布时间】: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 之前不存在,我检查了一下,这不是问题。

标签: android layout


【解决方案1】:

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

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));
}

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

【讨论】:

  • 感谢您的解决方案!但需要在 pre-JellyBean 上使用setBackgroundDrawable
  • 有一个更简单的方法here,并且无需硬编码……
【解决方案2】:

试试这条线

rootLayout.setBackgroundResource(d);

而不是

rootLayout.setBackgroundDrawable(d);

【讨论】:

    【解决方案3】:

    试试这个

    rootLayout.setBackgroundResource(R.drawable.image);
    

    【讨论】:

      【解决方案4】:

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

      在这种情况下,您只需在选中项目时调用 getListView().setItemChecked(position, true); 并在未选中时调用 getListView().setItemChecked(position, false);

      【讨论】:

        【解决方案5】:

        请尝试以下代码。

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

        【讨论】:

          【解决方案6】:

          你可以使用这样的东西

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-08-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-15
            • 2014-10-25
            相关资源
            最近更新 更多