Changing a divider with setDivider in a ListActivity without a custom ListView?

前端 未结 3 1595
无人共我
无人共我 2021-02-01 07:53

I can\'t seem to get a customized divider, using a Drawable I\'ve defined, to work when using a ListActivity and not creating a custom ListView. It al

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-01 08:40

    I figured it out. The issue had nothing to do with the ListActivity generating a ListView for me. It was in how I was defining the divider in Java code.

    There are two ways that I see to define the divider (border between ListView rows) on a ListView that is automatically inflated from a ListActivity, if you want to define the color in XML:

    Method 1:

    In res/values/colors.xml, put the following:

    
     #cceebb
    
    

    In your ListActivity-extending class, do this:

    ListView lv = getListView();
    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.color.sage));
    lv.setDivider(sage);
    lv.setDividerHeight(1);
    

    Method 2:

    In res/values/colors.xml:

    
     #cceebb
    
    

    And in your class that extends ListActivity:

    ListView lv = getListView();
    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.drawable.sage));
    lv.setDivider(sage);
    lv.setDividerHeight(1);
    

提交回复
热议问题