How to change text color of simple list item

后端 未结 12 2066
清酒与你
清酒与你 2020-11-27 04:12

I have an ListActivity and i am displaying one list with:

setListAdapter(new ArrayAdapter(getApplicationContext(),
                android.R.la         


        
相关标签:
12条回答
  • 2020-11-27 04:47

    Another simplest way is to create a layout file containing the textview you want with textSize, textStyle, color etc preferred by you and then use it with the ArrayAdapter.

    e.g. mytextview.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv"
        android:textColor="@color/font_content"
        android:padding="5sp"
        android:layout_width="fill_parent"
        android:background="@drawable/rectgrad"
        android:singleLine="true"
        android:gravity="center"
        android:layout_height="fill_parent"/>
    

    and then use it with your ArrayAdapter as usual like

    ListView lst = new ListView(context);
    String[] arr = {"Item 1","Item 2"};
    ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview,arr);
    lst.setAdapter(ad);
    

    This way you won't need to create a custom adapter for it.

    0 讨论(0)
  • 2020-11-27 04:48

    The simplest way to do this without needing to create anything extra would be to just modify the simple list TextView:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, android.R.id.text1, strings) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    TextView textView = (TextView) super.getView(position, convertView, parent);
                    textView.setTextColor({YourColorHere});
                    return textView;
                }
            };
    
    0 讨论(0)
  • 2020-11-27 04:50

    You just have override the getView method of ArrayAdapter

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), 
            android.R.layout.simple_list_item_1, mStringList) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = super.getView(position, convertView, parent);
            TextView text = (TextView) view.findViewById(android.R.id.text1);
            text.setTextColor(Color.BLACK);
            return view;
        }
    };
    
    0 讨论(0)
  • 2020-11-27 04:51

    you can use setTextColor(int) method or add style to change text color.

    <style name="ReviewScreenKbbViewMoreStyle">
    <item name="android:textColor">#2F2E86</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">10dip</item>
    

    0 讨论(0)
  • 2020-11-27 04:51

    Create an xml file in res/values and copy the below code

    <style name="BlackText">
    <item name="android:textColor">#000000</item>
    </style>
    

    and the specify the style in activity in Manifest like below

     android:theme="@style/BlackText"
    
    0 讨论(0)
  • 2020-11-27 04:54

    Try this code in stead of android.r.layout.simple_list_item_single_choice create your own layout:

    <?xml version="1.0" encoding="utf-8"?>
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_vertical"
        android:padding="5dip"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/black"
        android:textStyle="bold">
    </CheckedTextView>
    
    0 讨论(0)
提交回复
热议问题