【问题标题】:Android SetBackgroundResource & SetBackground not working pre lollipopAndroid SetBackgroundResource & SetBackground 在棒棒糖前不工作
【发布时间】:2015-12-24 02:01:59
【问题描述】:

我正在使用基于 Lollipop 的 avd 开发我的应用程序,其中我使用 SetBackgroundResource 使用自定义可绘制对象(图层列表)设置列表视图行的背景。它工作得很好。

LinearLayout xml 行具有默认定义的蓝色背景。

Jelly Bean 和 Kit Kat,使用 SetBackgroundResource 删除默认蓝色,但不会将其设置为请求的背景可绘制对象。在将 minsdk 从 15 更改为 16 后,我也尝试了 SetBackground。

还是不开心。

((LinearLayout)v.getParent()).setBackgroundResource(R.drawable.border_purple);

我也试过了;

((LinearLayout)v.getParent()).setBackground(ContextCompat.getDrawable(context,R.drawable.border_orange));

行 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/border_blue"
android:padding="12dp"
android:layout_marginBottom="16dp"
android:id="@+id/layout_row">
<TextView
    android:layout_height="wrap_content"
    android:id="@+id/item_title"
    android:text=""
    android:layout_width="match_parent"
    android:textSize="20sp"
    android:textColor="@color/primary_dark_material_light"
    android:background="#FFFFFF"/>
<View
    android:layout_width="match_parent"
    android:layout_height="2px"
    android:background="#D3DFE3" />
<TextView
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:id="@+id/item_byline"
    android:text=""
    android:background="#FFFFFF"/>
</LinearLayout>

【问题讨论】:

  • 你试过了吗:((LinearLayout)v.getParent()).setBackgroundResource(getResources().getDrawable(R.drawable.border_purple));
  • 我试过 context.getResources().getDrawable(R.drawable.border_blue) 产生错误 int cannot be applied to drawable 并且 getResources without context 产生错误 cannot resolve 方法。

标签: android xml layout android-studio setbackground


【解决方案1】:

经过进一步调查,它正在工作,但它从 LinearLayout 中删除了填充,因此,它似乎没有工作。我相信它也从图层列表中删除了填充。

【讨论】:

  • 它在我的情况下不起作用......同时使用了 sideline1.setBackgroundResource(R.color.gray_connector); sideline1.setBackgroundColor(getResources().getColor(R.color.gray_connector));
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 2015-05-07
  • 2016-02-11
  • 2015-01-12
  • 2015-09-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
相关资源
最近更新 更多