【问题标题】:textView resizes to longest TextView in the tableLayouttextView 调整为 tableLayout 中最长的 TextView
【发布时间】:2013-07-23 07:25:06
【问题描述】:

当我将 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);

在添加更长的文本之前

添加长文本后

【问题讨论】:

  • if(true){...;} ? :D

标签: java android textview tablelayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    阅读TableLayout 上的文档,我发现了这个:

    列的宽度由单元格最宽的行定义 那一栏。但是,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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多