在没有自定义 ListView 的 ListActivity 中使用 setDivider 更改分隔线?
当使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想通了。这个问题与 ListActivity 为我生成 ListView 无关。这是我在 Java 代码中定义分隔符的方式。
如果您想在 XML 中定义颜色,我认为有两种方法可以在从 ListActivity 自动膨胀的 ListView 上定义分隔线(ListView 行之间的边框):
方法 1:
在 res/values/colors.xml 中,输入以下内容:
在您的 ListActivity 扩展类中,执行以下操作:
方法 2:
在 res/values/colors.xml 中:
在扩展 ListActivity 的类中:
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:
In your ListActivity-extending class, do this:
Method 2:
In res/values/colors.xml:
And in your class that extends ListActivity:
以编程方式在列表视图中设置分隔符:
这些代码放入您的 .java 类
创建 Drawable 中:{res >可绘制> drawable_divider.xml}
To set divider in listview programatically:
These code put inside in your .java Class
Creating Drawable: {res > drawable > drawable_divider.xml}
试试这个代码:
Try this code: