代码之家  ›  专栏  ›  技术社区  ›  hotshot309

在没有自定义ListView的ListActivity中更改带有setDivider的分隔符?

  •  14
  • hotshot309  · 技术社区  · 14 年前

    我似乎无法使用自定义分隔符,使用我定义的可拉伸分隔符,在使用 ListActivity ListView . 当虚拟机创建自己的 对我来说 列表活动 ,它使用一个提供了默认分隔符的主题;如果我尝试提供一个分隔符,则 完全。

    我知道我可以创造一个习惯 列表视图 使用XML并定义android:divider ,这确实认可了我定制的可拉伸分隔条。但我宁愿让 列表活动 列表视图

    下面是我现在使用的代码:

    public class Categories extends ListActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            final String[] OPTIONS = {
                "Hello",
                "Goodbye",
                "Good Morning",
                "Greetings",
                "Toodaloo"
            };
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_1, OPTIONS);
            setListAdapter(adapter);
    
            ListView lv = getListView();
            PaintDrawable sage = new PaintDrawable(R.drawable.sage);
            lv.setDivider(sage);
            lv.setDividerHeight(1);
        }
    }
    
    3 回复  |  直到 12 年前
        1
  •  38
  •   hotshot309    14 年前

    我想出来了。这个问题与ListActivity为我生成ListView无关。我是如何在Java代码中定义分隔符的。

    如果要用XML定义颜色,我看到有两种方法可以定义从ListActivity自动膨胀的ListView上的分隔符(ListView行之间的边框):

    在res/values/colors.xml中,输入以下内容:

    <resources>
     <color name="sage">#cceebb</color>
    </resources>
    

    在ListActivity扩展类中,执行以下操作:

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

    方法2:

    在res/values/colors.xml中:

    <resources>
     <drawable name="sage">#cceebb</drawable>
    </resources>
    

    ListView lv = getListView();
    ColorDrawable sage = new ColorDrawable(this.getResources().getColor(R.drawable.sage));
    lv.setDivider(sage);
    lv.setDividerHeight(1);
    
        2
  •  3
  •   Dhruv Raval    10 年前

    以编程方式在listview中设置分隔符 :

    这些代码放在 .java语言 等级

       ListView lv = (ListView) findViewById(R.id.lv);
       lv.setDivider(getResources().getDrawable(R.drawable.drawable_divider));
       lv.setDividerHeight(1);
    

    创建可拉伸 可绘制分隔符.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:shape="rectangle">
    
        <solid android:color="#ececec"></solid>
    
    </shape>
    
        3
  •  3
  •   Dinithe Pieris jenzz    7 年前

    请尝试以下代码:

    searchText.setBackgroundColor(getResources().getColor(R.color.wordColorBlack));
    ListView lv = getListView();
    lv.setDivider(getResources().getDrawable(R.drawable.divider2));
    lv.setDividerHeight(2);