2016-12-30 95 views

回答

1

試試這個對我有用 我做了什麼是我有一個任務列表的任務與灰色的顏色和奇怪的單元格與白色的顏色。這段代碼符合你的目的。 執行以下步驟:

java文件代碼在下面。這是適配器類getView方法的代碼。在這裏我已經使用甚至奇怪的概念。它適用於我所有的Android版本。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View listViewItem = inflater.inflate(R.layout.list_task_data, null, true); 
     TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName); 
     TextView taskStatusImage = (TextView) listViewItem.findViewById(R.id.taskStatusImage); 
     TextView textSerialNumber = (TextView) listViewItem.findViewById(R.id.textSerialNumber); 
     LinearLayout linearLayout = (LinearLayout) listViewItem.findViewById(R.id.firstLayout); 


     if (position % 2 != 0) { 
      linearLayout.setBackgroundResource(Color.GRAY); 


     } else { 
      linearLayout.setBackgroundResource(Color.WHITE); 
     } 
} 

膨脹的佈局文件如下。

list_task_data.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/firstLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:weightSum="100"> 

    <TextView 
     android:id="@+id/textSerialNumber" 
     android:layout_width="37dp" 
     android:layout_height="37dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:gravity="center" 
     android:text="text" 
     android:textAlignment="center" 
     android:textColor="@color/black" 
     android:textSize="15dp" /> 

    <TextView 
     android:id="@+id/textViewName" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="30" 
     android:descendantFocusability="blocksDescendants" 
     android:fontFamily="@string/font_family_universal" 
     android:padding="5dp" 
     android:text="New Text" 
     android:textAlignment="center" 
     android:textIsSelectable="false" 
     android:textSize="20dp" 
     /> 

    <TextView 
     android:id="@+id/taskStatusImage" 
     android:layout_width="35dp" 
     android:layout_height="35dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="10dp" 
     android:gravity="center" 
     android:text="" 
     android:textAlignment="center" 
     android:textColor="@color/black" 
     android:textSize="10dp" /> 

</LinearLayout> 

在上面的佈局代碼中有你的列表視圖中的細胞膨脹的東西。 也可以添加代碼,以便根據選擇在其他條件中更改文本顏色字體大小等其他屬性。

希望它可以幫助你解決其工作示例代碼。

+0

該機制應該是相同的選擇器....只有選擇列表得到設計 –

+0

在我的代碼我列出了一組數據列表查看並根據偶數/奇數單元的概念爲單元格賦予背景顏色,所以如果您想爲特定單元格的背景着色,那麼您可以添加邏輯,然後可以使用我的代碼着色並突出顯示。 –

+0

你編碼得好...但我需要選擇器而不是實際添加邏輯和代碼 –

相關問題