我有一个两列TableLayout
作为滚动视图的唯一子。第一列包含TextViews
('labels'),第二列包含EditText
/ Spinner
/ DateWidget
等('values')。即使我已经为android:layout_width="fill_parent"
,TableLayout
和所有小部件指定了TableRow
(在'values'列中)。
创建活动时,屏幕看起来很完美。但是,当在EditText
中键入一个非常长的值时,“值”列超出了可见的屏幕区域。
我该如何解决这个问题?
您可能希望使用权重来定义列的大小。因此,您将定义表格布局高度以填充父级,但对于每列,您应将宽度设置为“0px”,将权重设置为希望列跨越的百分比。因此,假设您希望第一列为屏幕宽度的30%,则将其权重设置为“0.3”,将第二列设置为“0.7”。
试一试,看看是否有效。
解决这个问题的实用方法是在stretchColumns
中使用shrinkColumns
和TableLayout
属性。它们的值应该是基于0的列索引。
例如,如果您有两列表格布局,并且:
TableLayout
适合大屏幕设备上的整个父视图你会将你的TableLayout
定义为:
<TableLayout
android:id="@+id/my_table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:shrinkColumns="0" >
</TableLayout>
这对我有用..
tableLayout.setColumnShrinkable(1,true);
<TableLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<TableRow
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:text="Name "
android:layout_height="fill_parent">
</TextView>
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:hint="name"
android:layout_height="wrap_content"
>
</EditText>
</TableRow>
</TableLayout>
这段代码可以让我在第一列上对齐文本视图并编辑文本填充父级。
试试这个,确保android:singleLine="false"
和android:inputType="textMultiLine"
:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:text="Label:" />
<EditText android:id="@+id/entry" android:inputType="textMultiLine"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:singleLine="false" />
</TableRow>
</TableLayout>
Android 2.2上的EditText也有类似的问题。在我的情况下添加android:maxWidth =“0sp”属性帮助。现在EditText字段显示为我想要的 - 因此它仍然与其他EditText字段大小相同,但另外,当输入长文本时,它不会调整大小。
这是我的EditText定义:
<EditText android:id="@+id/extra_edit"
android:padding="3dip"
android:inputType="textImeMultiLine"
android:maxWidth="0sp"
android:layout_width="fill_parent"
/>