Android 更改文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10633542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:26:15  来源:igfitidea点击:

Changing the text color

androidandroid-layout

提问by hamzarh

I have a white background but with this layout i am blocked because text color is white too and i can't find a solution to make it red so i can set my white background, any help please (as u can see i am forced to use a red background her so i can see the white text).

我有一个白色的背景,但这个布局我被阻止了,因为文本颜色也是白色的,我找不到一个解决方案使它变成红色,所以我可以设置我的白色背景,请任何帮助(如你所见,我被迫使用红色背景她,所以我可以看到白色文字)。

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);
    }
}

回答by vnshetty

Best trick is you copy content layout file from android.R.layout.simple_list_item_multiple_choiceand make your own layout(my_list_view and edit the xml file and change the text color.

最好的技巧是您复制内容布局文件android.R.layout.simple_list_item_multiple_choice并制作自己的布局(my_list_view 并编辑 xml 文件并更改文本颜色。

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

适配器 = 新 ArrayAdapter(this,my_list_view, listItems);

Edit save this file as my_list_view;

编辑保存此文件为 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
/>

回答by sivi

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

回答by Hip Hip Array

You can set it in the XML layout:

您可以在 XML 布局中设置它:

<TextView 
 ...
 ...
 android:textColor="#FF0000" >
 </TextView>

Or programmatically:

或以编程方式:

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

回答by Dheeresh Singh

better to write custom adpter .

最好编写自定义适配器。

alternate but not good soltuon is use this layout xml inspace of android.R.layout.simple_list_item_multiple_choice

替代但不是很好的解决方案是在 android.R.layout.simple_list_item_multiple_choice 的空间中使用此布局 xml

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

This is same android.R.layout.simple_list_item_multiple_choice layout but your are now making same in your layouts with same ids to make the changes in it.

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

回答by vishesh chandra

you can try custom list view.. follow the link click here

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

回答by Sanjiv Patel

<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" 
/>