3

我有一个布局问题。我要做的是:

  1. 在 xml 中创建具有零个子项的 TableLayout:
    <TableLayout android:id="@+id/t_layout_contents" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/l_layout_tags" android:stretchColumns="1" android:paddingLeft="5dip" android:paddingRight="5dip" />
  2. 在 onCreate() 中以编程方式插入第一行:
    TableLayout tLayoutContents = (TableLayout)findViewById(R.id.t_layout_contents);
    NoteElement nr_1 = new NoteElement(this);
    tLayoutContents.addView(nr_1);

“NoteElement”类扩展了 TableRow。第一行仅包含一个空白 ImageView 作为占位符和一个 EditText 用于输入文本。NoteElement 的构造函数如下所示:

public NoteElement(Context c) {  
    super(c);  
    this.context = c;  
    defaultText = c.getResources().getString(R.string.create_note_help_text);  

    imageView = new ImageView(context);
    imageView.setImageResource(android.R.color.transparent);

    LayoutParams params = new LayoutParams(0);
    imageView.setLayoutParams(params);

    addView(imageView);
    addView(addTextField());
}

方法 addTextField() 指定 EditText 小部件的属性:

private EditText addTextField() {  
    editText = new EditText(context);  
    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);  
    editText.setMinLines(4);  
    editText.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    editText.setHint(R.string.create_note_et_blank_text);
    editText.setAutoLinkMask(Linkify.ALL);
    editText.setPadding(5, 0, 0, 0);
    editText.setGravity(Gravity.TOP);
    editText.setVerticalScrollBarEnabled(true);

    LayoutParams params = new LayoutParams(1);
    editText.setLayoutParams(params);

    return editText;
}

到现在为止还挺好。但是一旦字符的可用空间耗尽,我的问题就会出现。EditText 不会自行调整大小,而是切换到单行 EditText。我正在拼命寻找一种方式,在该方式中 EditText 动态调整其高度,这取决于插入的文本长度。

有人对此有提示吗?

4

2 回答 2

1

好的,我明白了。这似乎是一个TableLayout普遍的问题。我用一个简单的LinearLayout. 我同样达到目的,并且EditText正确显示。事实上,我看不到使用 a 的理由,TableLayout而且现在我想不出一个人真正需要它的情况,即 aLinearLayout是不够的。
所以我建议尽可能使用LinearLayout其他布局。RelativeLayout但请注意,这些只是我的两分钱......

于 2010-05-07T08:19:04.207 回答
1

我只是又看了一眼 TableLayout(出于完全不同的目的),偶然发现setColumnShrinkable(int columnIndex, boolean isShrinkable)它应该可以帮助我解决我以前的问题。

有关详细信息,请参阅文档:
https
://developer.android.com/reference/android/widget/TableLayout.html#setColumnShrinkable%28int,%20boolean%29 请注意,我没有对此进行测试。

于 2010-05-17T14:51:20.437 回答