【问题标题】:how to set textview dynamically according to numbers of strings如何根据字符串的数量动态设置textview
【发布时间】:2013-10-18 00:16:09
【问题描述】:

我正在从网络服务获取电话公司列表,我必须将其设置为 textview,但问题是我没有像上图那样对齐。如何实现它。

【问题讨论】:

  • 你得到了什么,你想要什么?请更清楚..
  • 我正在获取电话公司的列表,但我没有超出 textview 的对齐方式。
  • 对齐是一个非常通用的术语,特别是如果你展示一张图片,不同的人可能会有不同的解释..比如你在寻找左对齐还是行距还是什么?付出一些努力并尝试解释出了什么问题总是好的? “我没有超越对齐”不会帮助人们理解..
  • 可能会显示您当前正在获取的图片以及生成该视图的代码。
  • 我猜你必须为上图中的圆圈部分创建一个动态布局。要动态创建 n 个文本视图,请检查此链接 stackoverflow.com/a/5918524

标签: android textview


【解决方案1】:

据我了解,您希望将文本视图一个接一个地添加,但是当它们溢出(离开屏幕)时,下一个文本视图应该放在下一行。

这样做并非易事。实现这样的事情(最佳且正确)需要了解 android 如何绘制视图(onMeasureonLayout)。但是,如果您不太关心效率(主要是因为您只为一小部分视图执行此操作),那么这是我的快速破解:

mContainer = (RelativeLayout) findViewById(R.id.container);

// first layout all the text views in a relative layout without any params set.
// this will let the system draw them independent of one another and calculate the
// width of each text view for us.
for (int i = 0; i < 10; i++) {
    TextView tv = new TextView(getApplicationContext());
    tv.setText("Text View " + i);
    tv.setId(i+1);
    tv.setPadding(10, 10, 20, 10);
    mContainer.addView(tv);
}

// post a runnable on the layout which will do the layout again, but this time 
// using the width of the individual text views, it will place them in correct position.
mContainer.post(new Runnable() {
    @Override
    public void run() {
        int totalWidth = mContainer.getWidth();

        // loop through each text view, and set its layout params
        for (int i = 0; i < 10; i++) {
            View child = mContainer.getChildAt(i);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // this text view can fit in the same row so lets place it relative to the previous one.
            if(child.getWidth() < totalWidth) {

                if(i > 0) {  // i == 0 is in correct position
                    params.addRule(RelativeLayout.RIGHT_OF, mContainer.getChildAt(i-1).getId());
                    params.addRule(RelativeLayout.ALIGN_BOTTOM, mContainer.getChildAt(i-1).getId());
                }
            }
            else {
                // place it in the next row.
                totalWidth = mContainer.getWidth();
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                params.addRule(RelativeLayout.BELOW, mContainer.getChildAt(i-1).getId());
            }
            child.setLayoutParams(params);
            totalWidth = totalWidth - child.getWidth();
        }

        mContainer.requestLayout();
    }
});

基本上,我让系统在第一轮绘图中为我完成布局和测量。然后使用现在可用的每个文本视图的宽度,我根据包装逻辑重置布局参数并再次进行布局。

试试不同大小的文字,它会自动调整。我会说这个解决方案非常老套,但它确实有效。如果不满意take a look at this.

【讨论】:

    【解决方案2】:

    使用

     android:textAlignment="textStart"
    

    【讨论】:

    • 你能发布你得到的对齐图像吗
    猜你喜欢
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    相关资源
    最近更新 更多