Java textView将调整为tableLayout中最长的textView

Java textView将调整为tableLayout中最长的textView,java,android,textview,tablelayout,Java,Android,Textview,Tablelayout,当我将textView添加到tableLayout中的行时。。如果长度较长,则会调整其前面的所有对象的大小。我只想让每个textView都被包装成文本长度。。照片会解释得更好 TableLayout ll = (TableLayout) findViewById(R.id.messhistory); TextView edit = new TextView(this); TableRow row = new TableRow(this);

当我将textView添加到tableLayout中的行时。。如果长度较长,则会调整其前面的所有对象的大小。我只想让每个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);
在添加较长文本之前

添加长文本后


您在任何地方都使用了相同的
edit
实例,因此下次当您的文本变大时,它会将较大的文本包装起来,从而使其(编辑)以前的实例也变大。
一种可能的解决方案是在每次添加文本时创建一个新的edit实例

通过阅读有关
表格布局的文档,我发现:

列的宽度由列中单元格最宽的行定义 那个专栏。但是,TableLayout可以将某些列指定为 通过调用setColumnShrinkable()或 setColumnStretchable()。如果标记为可收缩,则可以调整列宽 可以收缩以使表适合其父对象。如果标记为 可拉伸,它可以扩展宽度,以适应任何额外的空间

所以,你注意到的行为是故意的。但要获得类似的效果(没有固定的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);
if(true){…;}
?:D