2016-12-30 31 views
-3

enter image description hereListViewのテキストの色と背景を変更する方法は?

上記の設計を実装したいと思います。私がこれにこだわっているのを助けてください。

+0

色を交互に変更したいですか? –

+0

背景と行の項目を設定 –

+0

バッターソリューション用にここにコードを投稿できますか?または、あなたがしたいことを正確に説明してください。 –

答えて

1

私がやったことは、灰色と奇妙な色の白い色で塗りつぶされた偶数のセルを持つタスクのリストがあることです。このコードはあなたの目的に合っています。 以下の手順を実行してください:

javaファイルコードは以下のとおりです。これはアダプタクラスのgetViewメソッドのコードです。ここで私はeven odd.Itのコンセプトを使って、すべてのアンドロイドバージョンでうまく動作します。

@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> 

のためのコードは、リストビューのセルに膨らま何があります。 また、選択肢ごとにif else条件でテキストの色のフォントサイズなどの他のプロパティを変更するコードを追加することもできます。

希望のサンプルコードを参考にしてください。

+0

メカニズムはセレクタと同じでなければなりません....選択されたリストのみがデザインを取得します –

+0

私のコードでリスト偶数/奇数セルのコンセプトに基づいてセルに背景色を与えたので、特定のセルの背景を色付けしたい場合は、ロジックを追加して、それを色付けし強調表示するコードを使用することができます。 –

+0

あなたは十分にコード化されていますが、ロジックとコードを実際に追加するのではなくセレクタが必要です –

関連する問題