在没有自定义 ListView 的 ListActivity 中使用 setDivider 更改分隔线?

发布于 2024-09-29 20:50:42 字数 1131 浏览 6 评论 0原文

当使用 ListActivity 而不是创建自定义 ListView 时,我似乎无法使用我定义的 Drawable 获得自定义分隔线。看起来,当虚拟机通过 ListActivity 为我创建自己的 ListView 时,它使用了一个带有默认分隔符的主题;如果我尝试提供一个,ListView 中根本不会出现分隔符。

我知道我可以使用 XML 创建自定义 ListView 并在该 ListView 上定义 android:divider,这确实可以识别我的自定义分隔符 Drawable。但我更愿意让 ListActivity 创建自己的 ListView,如果我能弄清楚如何让我自己的分隔符在其上工作的话。

这是我现在使用的代码:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风渺 2024-10-06 20:50:42

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

如果您想在 XML 中定义颜色,我认为有两种方法可以在从 ListActivity 自动膨胀的 ListView 上定义分隔线(ListView 行之间的边框):

方法 1:

在 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>

在扩展 ListActivity 的类中:

ListView lv = getListView();
ColorDrawable sage = new ColorDrawable(this.getResources().getColor(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);
最好是你 2024-10-06 20:50:42

以编程方式在列表视图中设置分隔符

这些代码放入您的 .java

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

创建 Drawable 中:{res >可绘制> drawable_divider.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>

To set divider in listview programatically:

These code put inside in your .java Class

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

Creating Drawable: {res > drawable > drawable_divider.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>
千纸鹤 2024-10-06 20:50:42

试试这个代码:

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

Try this code:

searchText.setBackgroundColor(getResources().getColor(R.color.wordColorBlack));
ListView lv = getListView();
lv.setDivider(getResources().getDrawable(R.drawable.divider2));
lv.setDividerHeight(2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文