How to change text color of simple list item

后端 未结 12 2065
清酒与你
清酒与你 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:30

    In simple word "you can't do it through simple setListAdapter" . you must used custom listview for freely changes in text color or in any other views

    for Custom Listview you can go with this link

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

    try this code...

    <RelativeLayout 
             xmlns:android="http://schemas.android.com/apk/res/android" 
             android:layout_width="fill_parent" 
             android:layout_height="fill_parent" 
             android:background="#ffff00"> 
    
        <ListView 
                android:id="@+id/android:list" 
                android:layout_marginTop="2px" 
                android:layout_marginLeft="2px" 
                android:layout_marginRight="2px"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:layout_weight="1" 
                android:background="@drawable/shape_1" 
                android:listSelector="@drawable/shape_3" 
                android:textColor="#ffff00" 
                android:layout_marginBottom="44px" /> 
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-27 04:34

    If you are creating a class that extends an Adapter, you can use parent variable to obtain the context.

    public class MyAdapter extends ArrayAdapter<String> {
    private Context context;
       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
           context = parent.getContext();
           context.getResources().getColor(R.color.red);
           return convertView;
       }
    }
    

    You can do the same with RecyclerView.Adapter, but instead of getview() you will use onCreateViewHolder().

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

    I'm also faced this situation. You can handle above method. but simple if you pass simple_dropdown_item_1line as resource. It will be black color text by default.

    This is my code I have achieved in kind of situation.

    ArrayAdapter<String> arrAdapter = 
    new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line,countryList);
            listView.setAdapter(arrAdapter);
    

    Your code below:

    setListAdapter(new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_dropdown_item_1line, mStringList));
    

    Actually android.R.layout.simple_dropdown_item_1line is used for dropdown. I urge u to see this layout content once. It just simple textview. thats all. It's property will not affect your task. I checked It works fine.

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

    If you want to keep all the style but change few details, you can use the default style defined on the Android and change what you want

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:textColor="@android:color/background_light"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:background="?android:attr/activatedBackgroundIndicator"
        android:minHeight="?android:attr/listPreferredItemHeightSmall" />
    

    Then set the adapter using:

    setListAdapter(new ArrayAdapter<String>(getApplicationContext(),
                R.layout.list_item_custom, mStringList));
    

    Source: https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/layout/simple_list_item_activated_1.xml

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

    I realize this question is a bit old but here's a really simple solution that was missing. You don't need to create a custom ListView or even a custom layout.

    Just create an anonymous subclass of ArrayAdapter and override getView(). Let super.getView() handle all the heavy lifting. Since simple_list_item_1 is just a text view you can customize it (e.g. set textColor) and then return it.

    Here's an example from one of my apps. I'm displaying a list of recent locations and I want all occurrences of "Current Location" to be blue and the rest white.

    ListView listView = (ListView) this.findViewById(R.id.listView);
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = (TextView) super.getView(position, convertView, parent);
    
            String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
            int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
            textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));
    
            return textView;
        }
    });
    
    0 讨论(0)
提交回复
热议问题