简体   繁体   English

textView调整为tableLayout中最长的TextView

[英]textView resizes to longest TextView in the tableLayout

When i add a textView to a row in a tableLayout.. it resizes all the ones before it, if it is longer in length. 当我将textView添加到tableLayout中的一行时...如果它的长度更长,它会调整之前的所有值。 all i want it is, for every textView to be wrapped to text length.. the pics will explain better 所有我想要的是,每个textView被包装到文本长度..图片将更好地解释

        TableLayout ll = (TableLayout) findViewById(R.id.messhistory);
        TextView edit = new TextView(this);
        TableRow row = new TableRow(this);
        //edit.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        edit.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
        if(true)
        {
            ImageView iv= new ImageView(this);
            row.addView(iv);
            iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        }
        row.addView(edit,new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
        ll.addView(row);

Before adding a longer text 在添加更长的文本之前

在此输入图像描述

After adding a long text 添加长文本后

在此输入图像描述

You are using same instance of edit everywhere, so next time when you get your text larger, it wraps the larger text, so it makes its(edit) previous instances larger as well. 您在任何地方都使用相同的edit实例,因此下次当您将文本放大时,它会包装较大的文本,因此它的(编辑)前一个实例也会变大。 One possible solution is to create a new instance of edit each time you add your text. 一种可能的解决方案是每次添加文本时创建一个新的编辑实例。

Reading through the documentation on TableLayout , I came acrross this: 通过阅读TableLayout上的文档,我得到了以下内容:

The width of a column is defined by the row with the widest cell in that column. 列的宽度由该列中具有最宽单元格的行定义。 However, a TableLayout can specify certain columns as shrinkable or stretchable by calling setColumnShrinkable() or setColumnStretchable(). 但是,TableLayout可以通过调用setColumnShrinkable()或setColumnStretchable()将某些列指定为可缩小或可伸缩的。 If marked as shrinkable, the column width can be shrunk to fit the table into its parent object. 如果标记为可收缩,则可以缩小列宽以使表适合其父对象。 If marked as stretchable, it can expand in width to fit any extra space. 如果标记为可拉伸,则可以扩展宽度以适应任何额外空间。

So, the behavior you are noticing is by design. 所以,你注意到的行为是设计的。 But to get a similar effect(without the fixed cloumn width), give this code a try: 但要获得类似的效果(没有固定的cloumn宽度),请尝试使用此代码:

// Define a Linearlayout instead of a TableLayout in your layout file
// Set its width to match_parent
// Set its orientation to "vertical"
LinearLayout ll = (LinearLayout) findViewById(R.id.someLinearLayout);

// This serves the same purpose as the TableRow
LinearLayout llRow = new LinearLayout(this);

TextView edit = new TextView(this);

edit.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));

ImageView iv= new ImageView(this);

llRow.addView(iv);

iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

LinearLayout.LayoutParams  llLeft = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout.LayoutParams  llRight = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);

llLeft.gravity = Gravity.LEFT;

llRight.gravity = Gravity.RIGHT;

// You can set the LayoutParams llLeft(messages appear on left) or 
// llRight(messages appear on right) Add "edit" first to make the imageview appear on right

llRow.addView(edit, llLeft);

ll.addView(llRow);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM