改变文本颜色

7

我的背景是白色的,但是由于文本颜色也是白色的,所以我被这个布局给阻挡住了,我找不到解决办法来将它变为红色,这样我才能设置我的白色背景。请帮忙解决一下(正如你所看到的,我被迫在这里使用红色背景,这样我才能看到白色的文本)。

public class QueueListActivity extends ListActivity {
// LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
ArrayList<String> listItems = new ArrayList<String>();
String newtext;
String listFiles;
// DEFINING STRING ADAPTER WHICH WILL HANDLE DATA OF LISTVIEW
ArrayAdapter<String> adapter;

// RECORDING HOW MUCH TIMES BUTTON WAS CLICKED
int clickCounter = 0;

ArrayList<String> selectedItems = new ArrayList<String>();


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.queuelistactivity);
    Bundle extras1 = getIntent().getExtras(); 
    listFiles=GetFiles();

    StringTokenizer tokonizer1 = new StringTokenizer(listFiles,";");
    while(tokonizer1.hasMoreElements()){
        Log.i("verif","0");
        listItems.add(tokonizer1.nextToken());}
          initializeListItems();



    if (extras1 != null) {
        newtext = extras1.getString("newitem");
        listItems.add(newtext);
        adapter.notifyDataSetChanged();
        getListView().setItemChecked(listItems.size() - 1, false);

    }




}



// METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItems(View v) {

    Intent intent = new Intent(QueueListActivity.this, AjouterFiles.class);     
    QueueListActivity.this.startActivity(intent);


    /*
    listItems.add(userName);
    adapter.notifyDataSetChanged();
    getListView().setItemChecked(listItems.size() - 1, false);*/


}

public void deleteItems(View v) {
    String toDelete = "";
    SparseBooleanArray sp = getListView().getCheckedItemPositions();

    for (int i = 0; i < sp.size(); i++) {
        toDelete += ";" + sp.get(i);
        if (sp.get(i)) {
            listItems.remove(i);
        }

    }
    adapter.notifyDataSetChanged();
    Toast.makeText(getApplicationContext(), toDelete, Toast.LENGTH_LONG).show();
    initializeListItems();
}

private void initializeListItems() {
    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, listItems);
    setListAdapter(adapter);
    ListView lv = getListView();
    lv.setCacheColorHint(Color.rgb(0, 0, 0));
    lv.setBackgroundColor(Color.rgb(178, 34, 34));


    for (int i = 0; i < lv.getCount(); i++) {
        lv.setItemChecked(i, false);
    }
}
6个回答

13

最好的方法是从android.R.layout.simple_list_item_multiple_choice中复制内容布局文件,然后创建自己的布局(my_list_view),编辑XML文件并更改文本颜色。

adapter = new ArrayAdapter(this, my_list_view, listItems);

编辑保存此文件为my_list_view;

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"
    android:background="#FFFFFF" //white background
    android:textColor="#000000" //black color text
/>

我该如何找到android.R.layout.simple_list_item_multiple_choice? - hamzarh
1
你可以直接在 Android.R.layout.simple_list_item_multiple_choice 上按 Ctrl+鼠标悬停,但我已经复制了那段代码。 - vnshetty
1
你正在使用哪个SDK?请尝试使用fill_parent。 - Dheeresh Singh
1
同意 @Dheeresh Singh 的建议,使用 fill_parent 而不是 match_parent。 - vnshetty
是的,现在没问题了,但在我的Java代码中出现了这个错误: 05-17 11:00:29.317: E/ArrayAdapter(987): 必须为TextView提供资源ID 05-17 11:00:29.366: E/AndroidRuntime(987): java.lang.IllegalStateException:ArrayAdapter要求资源ID是一个TextView。 - hamzarh
显示剩余2条评论

7
android:textColor="@android:color/white"

3
你可以在XML布局中设置它:
<TextView 
 ...
 ...
 android:textColor="#FF0000" >
 </TextView>

或者通过编程:

textview.setTextColor(Color.RED);
//textview must be defined in your class 

1
如果你能看到,我没有TextView!! - hamzarh

0

最好编写自定义适配器。

另一种选择,但不是很好的解决方案是在布局XML中使用此样式:android.R.layout.simple_list_item_multiple_choice。

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:paddingLeft="6dip"
    android:paddingRight="6dip"


   android:textColor= Color.Red       //please correct the syntax
/>

这是相同的android.R.layout.simple_list_item_multiple_choice布局,但现在您正在使用相同的ID在您的布局中进行相同的更改。


我创建一个新的XML文件并将其复制到哪里? - hamzarh
在布局中创建一个新的XML文件,可以使用任何名称,将此代码粘贴到该XML文件中,并在适配器中使用此XML文件代替android.R.layout.simple_list_item_multiple_choice。预定义的ArrayAdapter将查找getView中的id“text1”,并将其作为您在XML中也拥有的内容获取。 - Dheeresh Singh
我现在遇到了这个错误:错误:不允许使用字符串类型(在“layout_width”处的值为“match_parent”)。 - hamzarh
你正在使用哪个SDK?请尝试使用fill_parent。 - Dheeresh Singh

0
<TextView
        android:id="@+id/text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Name"
        android:textColor="@color/white"
        android:textSize="11dp" 
/>

0

你可以尝试自定义列表视图...请点击链接这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接