改变与setDivider分频器在ListActivity没有一个自定义的ListView?分频器、自定义、setDivider、ListView

2023-09-04 11:22:06 作者:浣熊大叔@

我似乎无法得到一个定制的分频器,用我已经定义了一个绘制对象,工作的时候使用 ListActivity ,而不是创建自定义的的ListView 。它似乎像在虚拟机创建自己的的ListView 对我来说,与 ListActivity ,它使用与主题默认的分隔符提供的;如果我试图提供的,没有任何分隔出现在的ListView 的。

我知道我可以创建自定义的的ListView 使用XML和定义的android:分上的ListView ,和这确实承认我的自定义分隔绘制对象。不过,我想preFER只是让 ListActivity 创建自己的的ListView ,如果我能想出​​如何得到我自己的分频器的工作就可以了。

这里的code,我现在使用:

 公共类分类扩展ListActivity {

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        最终的String []选项= {
            您好,
            再见,
            早上好,
            问候,
            Toodaloo
        };

        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(
            对此,android.R.layout.simple_list_item_1,OPTIONS);
        setListAdapter(适配器);

        ListView的LV = getListView();
        PaintDrawable鼠尾草=新PaintDrawable(R.drawable.sage);
        lv.setDivider(SAGE);
        lv.setDividerHeight(1);
    }
}
 

解决方案

我想它了。这个问题没有任何关系的ListActivity产生一个ListView我。这是我是如何界定的Java code分频器。

有,我看到定义在一个ListView是从一个ListActivity自动充气分频器(ListView的行之间的边界),如果要定义XML的颜色有两种方式:

方法1:

setContentView R.layout.spinneractivity 出错

在RES /价值/ colors.xml,把下面的:

 <资源>
 <颜色名称=圣人>#cceebb< /彩色>
< /资源>
 

在您的ListActivity延伸级,做到这一点:

 的ListView LV = getListView();
ColorDrawable鼠尾草=新ColorDrawable(this.getResources()的getColor(R.color.sage));
lv.setDivider(SAGE);
lv.setDividerHeight(1);
 

方法2:

在RES /价值/ colors.xml:

 <资源>
 <绘制NAME =圣人>#cceebb< /绘制>
< /资源>
 

而在你的类扩展ListActivity:

 的ListView LV = getListView();
ColorDrawable鼠尾草=新ColorDrawable(this.getResources()的getColor(R.drawable.sage));
lv.setDivider(SAGE);
lv.setDividerHeight(1);
 

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 almost seems like when the VM creates its own ListView for me, with the ListActivity, it uses a theme with the default divider provided; and if I try to provide one, no dividers appear in the ListView at all.

I know that I can create a custom ListView using XML and define android:divider on that ListView, and this does recognize my custom divider Drawable. But I would prefer to just let the ListActivity create its own ListView, if I can figure out how to get my own divider working on it.

Here's the code I'm using now:

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);
    }
}

解决方案

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:

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

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:

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

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);