2014-06-12 33 views
0

以下是我的代碼顯示簡單的警告對話框中,單擊一個按鈕時,如何更改提示對話框的列表視圖中的文本顏色?

AlertDialog.Builder newImage = new AlertDialog.Builder(MyActivity.this, AlertDialog.THEME_HOLO_LIGHT); 
newImage.setTitle("Select Image"); 
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> ( MyActivity.this, android.R.layout.simple_list_item_1); 
arrayAdapter.add("Take from camera"); 
arrayAdapter.add("Select from gallery"); 

newImage.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() 
{ 
    @Override 
    public void onClick(DialogInterface dialog, int which) 
    { 
    } 
}); 

此代碼,顯示輸出像下面的圖片,

enter image description here

這表明以白色列出項目,這就是爲什麼它不可見。我想將列表項的顏色更改爲黑色。

我該怎麼辦?

回答

3

試試這個

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> ( MyActivity.this, android.R.layout.simple_list_item_1) { 
    @Override 
    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.BLACK); 
    return view; 
    } 
}; 

簡單地覆蓋ArrayAdapter的getview,找到的TextView來改變顏色

+0

它給了我這個錯誤,'06-12 09:58:03.367:E/AndroidRuntime(21225):java.lang。IllegalStateException:ArrayAdapter需要資源ID爲TextView 06-12 09:58:03.367:E/AndroidRuntime(21225):\t at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386) 06-12 09: 58:03.367:E/AndroidRuntime(21225):\t at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)' – user2060383

+0

@ user2060383 eddited –

+0

你在說什麼Custom AlertDialog? – user2060383

1

只需使用您的自定義xml文件代替android.R.layout.simple_list_item_1並將其傳遞給ArrayAdapter。

像這樣:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ff00ff" 
     android:textSize="20sp" 
     android:text="TextView" /> 

</LinearLayout> 

而更改適配器:

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this,R.layout.Your_xml,R.id.textView1); 
+0

@ user2060383,可我知道什麼親在使用你自己的佈局而不是android中使用blem。 –

+0

爲什麼要自定義,當你可以做到本地?但我也喜歡你的答案,謝謝,+1 – user2060383

1
TextView txt1 = (TextView) v.findViewById(android.R.id.text1); 
txt1.setTextColor(Color.RED); 

UPDATE

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> ( MyActivity.this, android.R.layout.simple_list_item_1) { 
    @Override 
    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

Nah,它沒有用,它甚至沒有AlertDialog。 – user2060383

+1

OP應該把代碼放在哪裏?現在,這不是一個答案 – panini

+0

兄弟,這段代碼可以工作,將它放在適配器的'getView'中。 – Gunaseelan

0

需要在佈局定義custem警告對話框觀點一樣, ,然後再進行功能

LayoutInflater factory = LayoutInflater.from(YourCurrentclass.this); 
    View deleteDialogView = factory.inflate(R.layout.mylayout, null); 
    final AlertDialog deleteDialog = new AlertDialog.Builder(
      YourCurrentclass.this).create(); 
    deleteDialog.setView(deleteDialogView); 
    final TextView text = (TextView) deleteDialogView 
      .findViewById(R.id.textv2); 
    Button p = (Button) deleteDialogView.findViewById(R.id.plusbtn); 
    Button m = (Button) deleteDialogView.findViewById(R.id.minusbtn); 
p.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
        text.setTextColor(Color.BLACK); 
     } 
    }); 

你也可以把它從佈局(mylayout)更改當您創建自定義dilaoag像上面這個文件中。

+1

哦,夥計....正確地閱讀問題。 – user2060383

+0

ohhh對不起,我認爲他想要在對話框中執行功能...... – user3698393

+0

花點時間,並正確地閱讀問題,請。 – user2060383

0

用於更改文本顏色這種最簡單的方法:

創建的字符串值到string.xml文件夾值象下面

<string name="text1">Font color is <font fgcolor="#000000">Take from camera</font></string> 

,你可以通過這個使用這個字符串轉換適配器:

​​

希望這將work.Good運氣..

相關問題