我正在尝试使用androidPallete为代码中的线性布局设置自定义颜色。
代码如下:

public void changeColour(Pallete palette){
   v = li.inflate(R.layout.contact_info, null);
   contactInfo = (LinearLayout)v.findViewById(R.id.contact_layout);
   contactInfo.setBackgroundColor(palette.getDarkMutedSwatch().getBodyTextColor());
}

我也尝试过先创建ColorDrawable,然后用它设置这样的背景。
ColorDrawable cd = new ColorDrawable(palette.getDarkMutedSwatch().getBodyTextColor());
contactInfo.setBackground(cd);

但即使这样也不行。contactInfo是aListView的一个元素。
列表项的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="5dp"
        android:id="@+id/contact_layout"
        android:weightSum="3">

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/pic"
            android:src="@drawable/image" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="2.6"
            android:layout_margin="5dp"
            android:paddingLeft="10dp"
            android:layout_gravity="center">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                android:text="User name"
                android:textSize="24dp"
                android:textColor="#ffffff"/>


        </LinearLayout>

        <LinearLayout
            android:layout_weight="0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center"
            android:gravity="center"
            android:visibility="gone"
            android:orientation="horizontal">

            <CheckBox
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/check" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

最佳答案

我发现了问题。我改变了数组适配器中布局的颜色,它也改变了。解决方案如下。

 public View getView(int i, View convertView, ViewGroup viewGroup) {
       View view = convertView;
       view.findViewById(R.id.contact_layout).setBackgroundColor(Color.BLUE)

因此,通过将颜色通过一个对象数组传递给数组适配器,我能够解决我的问题。

07-24 09:44
查看更多