2013-07-30 27 views
8

私はコードを表示するために簡単なアダプタを使用しています。残念ながら、私はトップのtextViewの色を変更する必要があります。android.R.layout.simple_list_item_2のテキストの色を変更してください

これは私のコードの抜粋です:

// Keys used in Hashmap 
String[] from = { "txt1", "txt2" }; 
// Ids of views in listview_layout 
int[] ids = { android.R.id.text1, android.R.id.text2 }; 
SimpleAdapter adapter = new SimpleAdapter(this, aList, 
android.R.layout.simple_list_item_2, from, ids); 
setListAdapter(adapter); 

私は自分simple_list_item_2を作ってみましたが、それは私が何らかの理由でXMLでのTextViewの色を変更することができません。これを行う方法に関するアイデア?

私の最後の思考は次のとおりです。

findViewById(android.R.id.text1).setTextColor(#000)が、私はそれをどこに置くか分からない、と私の進コードは動作しません。

+0

16進数のカラーを渡すには、 'setTextColor(Color.parseColor("#YOURCOLOR "))'を使用する必要があります。ただし、これはカスタムアダプターなしでは機能しません。 –

答えて

1

は、あなたのListViewのアイテムのカスタムXMLレイアウトを作成し、textColor属性を使用してTextViewのテキストの色を設定します。

<TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="TextView" 
      android:textColor="#ff0000" /> 
16

あなたはSimpleAdapterからgetViewメソッドをオーバーライドする必要があります。例えば:あなたはスピナー、ドロップダウンテキストの色が変更されません使用している場合

SimpleAdapter adapter = new SimpleAdapter(this, aList, 
      android.R.layout.simple_list_item_2, from, ids) { 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 
      TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
      text1.setTextColor(Color.RED); 
      return view; 
     }; 
    }; 
+0

私のケースでは 'ViewGroupは型に解決できません ' – jaimin

+0

これは本当に奇妙な@jaiminです。新しい質問を投稿した方が良いでしょう。 – Blackbelt

+1

私はこれが良い解決策だとは思わない。アプリケーションロジックとスタイリングを混在させるべきではありません。カスタムレイアウトを@FD_ suggestとして定義する方が良いでしょう。 – Jeremy

-1

あなたはsetTextColor(Color.any color);

TextView txt = (TextView) view.findViewById(R.id.text1); 
txt.setTextColor(Color.yellow); 
0

を使用する必要があります。変更するには、メソッドgetDropDownViewも上記のメソッドに追加する必要があります。

public View getDropDownView (int position, View convertView, ViewGroup parent) { 
                 View view = super.getDropDownView (position, convertView, parent); 
                 TextView text = (TextView) view.findViewById (android.R.id.text1); 
                 text.setTextColor (Color.BLACK); 
                 return view; 
             } 
関連する問題