问题描述:

How do I style a specific item in a ListView? For example let's say

if(text in list view.equals("ABC")), then ABC should be bold, have a size of 16sp etc. I am creating this ListView from a String array.

Here's my code:

public class IngSearch extends AppCompatActivity implements AdapterView.OnItemClickListener, AbsListView.MultiChoiceModeListener {

ListView lv;

String list[] = {"123", "QAZ", ""edc", "ABC"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_ing_search);

lv = (ListView) findViewById(R.id.list1);

lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));

And in this list I want to apply a style like this to ABC

<style name="abcStyle">

<item name = "android:textSize">24sp</item>

<item name="android:textColor">#000</item>

</style>

Thanks

网友答案:

I think this is solution
Firstly, you need to custom your listview adapter then inside getView(...) you can set the style for each specific textview

      public class ListViewAdapter extends BaseAdapter {


            @Override
            public int getCount() {
               ...
            }

            @Override
            public codeLearnChapter getItem(int arg0) {
                ...
            }

            @Override
            public long getItemId(int arg0) {
               ...
            }

            @Override
            public View getView(int arg0, View arg1, ViewGroup arg2) {
             ...
             TextView textViewTitle = (TextView) findViewById(R.id.text_view_title);
             if(your condition){ 
                   textViewTitle.setTextAppearance(this, R.style.abcStyle); // change your textview style here
             }else if(...){
                   textViewTitle.setTextAppearance(// another style );
             }else{
                  textViewTitle.setTextAppearance(// another style );
             }
        }
}

Hope this help

相关阅读:
Top