以编程方式设置背景会覆盖drawable

时间:2013-08-05 00:50:30

标签: android android-layout android-listview android-drawable

我有一个ListViewDrawable中的每个项目都有ListView,以便在选中/按下时突出显示每一行。我也有一个自定义适配器,我以编程方式设置每行的背景颜色(我希望有交替的背景颜色)。这是我添加的一项新功能,在添加代码之前,行会突出显示蓝色,但是在它们不突出显示之后。不知道如何解决它。这就是我所拥有的:

ListView项目布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
    android:background="@drawable/app_selector"
>    
    <TextView  
        android:id="@+id/text"
        style="@style/ListingTitle"
        android:layout_alignParentTop="true"
        android:paddingBottom="2dip"
    />

</RelativeLayout>

绘制对象

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@color/light_blue" />

    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />

    <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />

    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />

</selector>

片段

public class ListingFragment extends SherlockListFragment
{
    public void onActivityCreated(final Bundle icicle)
    {    
        ListView lv = getListView();

        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        mListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

                @Override
                public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
                }

                @Override
                public boolean onActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item) {
                }
        }
    }

     /* ADAPTER */
     private class CustomAdapter extends BaseAdapter 
     {       
        @Override
        public View getView(final int position, View convertView, final ViewGroup parent)
        {
            final ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();

                convertView = mInflater.inflate(R.layout.item, parent, false);
            }
            else 
            {
                holder = (ViewHolder)convertView.getTag();
            }

            if (position % 2 == 0)
                convertView.setBackgroundColor(Color.GRAY);
            else
                convertView.setBackgroundColor(Color.TRANSPARENT);

            return(convertView);
        }
    }

}

2 个答案:

答案 0 :(得分:6)

扩展dum的答案,你不需要在代码中完成所有工作。

Drawable A

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/light_blue" />
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
</selector>

可绘制B

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/light_blue" />
    <item android:state_selected="true" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_activated="true" android:state_selected="false" android:state_pressed="false" android:drawable="@color/light_blue" />
    <item android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/gray" />
</selector>

在适配器中:

if (position % 2 == 0) {
    convertView.setBackgroundResource(R.drawable.A);
} else {
    convertView.setBackgroundResource(R.drawable.B);
}

答案 1 :(得分:0)

而不是使用普通颜色更改drawable,首先以编程方式创建一个新的drawable并像在xml中一样设置状态,然后为每个状态设置颜色。然后尝试使用以编程方式创建的drwable替换背景。