1

我一直在看东西,Stackoverflow无法找到如何做到这一点。

我想要的是这样的:

    xxxxxxxx
    --------
    xxxx
    --------
    xx

而不是通常的

    xxxxxxxx
    ----------------------------------- (until end of screen)
    xxxx
    -----------------------------------
    xx

我想知道是否有一种非常简单的方法可以做到这一点。

我认为这涉及更改右边距,但这就是我所得到的。

顺便说一句,我ListViews正在运行时创建我的。

4

3 回答 3

1

你应该用一个自定义的 drawable 做这样的事情:

<ListView
  android:divider="@drawable/fancy_gradient"
  android:dividerHeight="@dimen/divider_height"...

Java方式:

list = (ListView) findViewById(R.id.list);
int[] colors = {0, 0xFF97CF4D, 0};
ListView inner = list.getRefreshableView();
inner.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
inner.setDividerHeight(1);

但似乎分隔线宽度不可定制......

于 2013-07-10T12:11:04.267 回答
1

由于您不能以编程方式直接更改分隔线宽度,因此我建议编写一个适配器来获取您的字符串列表。适配器将由一个 TextView 和一个 ImageView(最好)组成。

在适配器中,您可以比较字符串并获得最长的字符串。通过以下方式获取最长字符串的估计宽度;

String longestText = "longestword";
Paint paint = new Paint();
float widthValue = paint.measureText(longestText);

(这将为您提供以像素为单位的结果)在获得此值后,您可以简单地将 (height=x , width=widthValue) 的图像视图放在文本视图下。

然后从 xml 中的列表视图中删除分隔符;

android:divider="@android:color/transparent"

并且您想要的视图已准备好使用。

于 2013-07-10T12:53:17.597 回答
0

我想它可以工作了。它似乎工作。这是我的做法。

     ListAdapter listAdapter = posList.getAdapter();
                //base case
                int longestWidth = listAdapter.getView(0, null, posList).getMeasuredWidth();
                for (int i = 0; i < listAdapter.getCount(); i++) {
                    View listItem = listAdapter.getView(i, null, posList);
                    listItem.measure(0, 0);
                    //check if the items in the list are longer than base case
                    if (listItem.getMeasuredWidth() > longestWidth)
                    {
                        longestWidth = listItem.getMeasuredWidth();
                    }

                }
                ViewGroup.LayoutParams params = posList.getLayoutParams();
                //assign width of textview to longest item in the list
                params.width = longestWidth;
于 2013-07-10T12:58:14.773 回答