简体   繁体   中英

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. all i want it is, for every textView to be wrapped to text length.. the pics will explain better

        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. 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:

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(). 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:

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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