【问题标题】:Dynamically resize TextView width in TableLayout在 TableLayout 中动态调整 TextView 的宽度
【发布时间】:2016-05-31 16:21:49
【问题描述】:

我已经动态创建了一个表,其中填充了不同长度的键和值。

当我尝试设置宽度以换行内容时,宽度是相对于单个键大小而不是整个表设置的。

这会导致第二个 TextView 太宽,一旦将具有较大宽度的键添加到表中。

我尝试在表格完成后重绘表格,但保留了原始的 TextView 宽度。

是否有一个我忽略的简单修复,或者我需要以编程方式获取最长的键宽度,然后手动设置第二个 TextView 的最大宽度?

layout.xml

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:scrollbars="none">

    <TableLayout
        android:id="@+id/passData"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="15dip">
    </TableLayout>
</ScrollView>

activity.java

private TableRow generateRow(String key, String value, boolean isOdd) {

    TableRow tr = new TableRow(this);
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));

    if (isOdd) {
        tr.setBackgroundColor(getResources().getColor(R.color.colorTableRow));
    }

    TextView keyView = new TextView(this);
    TextView valueView = new TextView(this);

    keyView.setTypeface(null, Typeface.BOLD);
    keyView.setPadding(30, 20, 30, 20);
    keyView.setTextSize(15);
    keyView.setText(firstCaps(key));

    valueView.setText(value.replace(" ", "\u00A0"));
    valueView.setSingleLine(false);
    valueView.setMaxLines(20);
    valueView.setPadding(0, 20, 30, 20);
    valueView.setGravity(Gravity.CLIP_HORIZONTAL);

    tr.addView(keyView);
    tr.addView(valueView,new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,     
TableRow.LayoutParams.WRAP_CONTENT));

    return tr;
}

结果

【问题讨论】:

    标签: android textview android-tablelayout


    【解决方案1】:

    解决方案是简单地为 LayoutParams 添加权重并将宽度设置为零以使其大小正确。

    activity.java

    private TableRow generateRow(String key, String value, boolean isOdd) {
        TableRow tr = new TableRow(this);
        tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
    TableRow.LayoutParams.WRAP_CONTENT));
        if (isOdd) {
            tr.setBackgroundColor(getResources().getColor(R.color.colorTableRow));
        }
        TextView keyView = new TextView(this);
        TextView valueView = new TextView(this);
    
        keyView.setTypeface(null, Typeface.BOLD);
        keyView.setPadding(30, 20, 30, 20);
        keyView.setTextSize(15);
        keyView.setText(firstCaps(key));
        valueView.setText(value);
        valueView.setSingleLine(false);
        valueView.setMaxLines(20);
        valueView.setPadding(0, 20, 30, 20);
        tr.addView(keyView);
    
        // Set width to zero and weight to 1
        tr.addView(valueView,new TableRow.LayoutParams(0, 
            TableRow.LayoutParams.WRAP_CONTENT, 1f));
    
        return tr;
    }
    

    结果

    【讨论】:

      【解决方案2】:

      您可以使用此 auto_resize_text 视图来解决您的问题。

      Auto Scale TextView Text to Fit within Bounds

      https://github.com/grantland/android-autofittextview

      【讨论】:

      • 不确定这是否可行,因为 TextView 的宽度已经设置。明天试试,让你知道。
      • 正如怀疑的那样 - TextView 的大小已经相对于行中其他 TextView 的宽度确定。因此这个类没有效果。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多