2015-09-26 46 views
1

我使用,我用SetBackgroundResource設置使用自定義繪製(層列表)一個列表視圖行的背景棒棒糖基於AVD發展我的應用程序。它工作得很好。的Android SetBackgroundResource及的setBackground工作不預棒棒糖

LinearLayout xml行具有默認的藍色背景。

果凍豆和Kit Kat,使用SetBackgroundResource刪除默認的藍色,但它不會將其設置爲請求的背景可繪製。我也在15到16之間更改minsdk後試過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> 
+0

你有沒有嘗試過這樣的:。 '((LinearLayout中)v.getParent())setBackgroundResource(getResources()getDrawable(R.drawable.border_purple));' –

+0

我試過context.getResources()getDrawable(R .drawable.border_blue)產生錯誤int不能被應用到drawable和getResources沒有上下文產生錯誤無法解析方法。 – tywsge

回答

0

經過進一步調查,這是工作,但因此移除了的LinearLayout填充,它似乎因爲它沒有工作。我相信它也從層列表中刪除了填充。

+0

它不工作在我的情況...使用sideline1.setBackgroundResource(R.color.gray_connector); sideline1.setBackgroundColor(getResources()的getColor(R.color.gray_connector)); –