根据列表中最长的文本更改 ListView 分隔线长度 [英] Change ListView divider length based on longest Text in List
问题描述
我一直在查看 Stackoverflow
上的内容,但不知道如何执行此操作.
I've been looking at things on Stackoverflow
and cannot find out how to do this.
我想要的是这样的:
xxxxxxxx
--------
xxxx
--------
xx
代替平常
xxxxxxxx
----------------------------------- (until end of screen)
xxxx
-----------------------------------
xx
我想知道是否有一种非常简单的方法可以做到这一点.
I wonder if there is an really easy way to do it.
我认为这涉及到更改右边距,但就我所知.
I figure it involves changing the right margin but that's about as far as I got.
顺便说一下,我正在运行时创建我的 ListViews
.
I am creating my ListViews
at run-time by the way.
推荐答案
由于您不能直接以编程方式更改分隔线宽度,我建议编写一个适配器来获取您的字符串列表.Adapter 将由一个 TextView 和一个 ImageView(最好)组成.
Since you can't directly change divider width programmatically, I suggest writing an adapter which takes your string list. Adapter will be consisting of one TextView and one ImageView(preferably).
在适配器中,您可以比较字符串并获得最长的字符串.获取该最长字符串的估计宽度;
In the adapter you can compare strings and get the longest one. Get estimated width for that longest string by;
String longestText = "longestword";
Paint paint = new Paint();
float widthValue = paint.measureText(longestText);
(这会给你以像素为单位的结果)在获得这个值后,你可以简单地在 textview 下放置一个 (height=x , width=widthValue) 的图像视图.
(that will give you the result in pixels) And after getting this value, you can simply put an imageview of (height=x , width=widthValue) under the textview.
然后从 xml 中的列表视图中删除分隔线;
Then remove the divider from your listview in xml by;
android:divider="@android:color/transparent"
您可以随时使用您想要的视图.
and your view as you desire is ready to use.
这篇关于根据列表中最长的文本更改 ListView 分隔线长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!