Android TableLayout宽度

问题描述 投票:32回答:6

我有一个两列TableLayout作为滚动视图的唯一子。第一列包含TextViews('labels'),第二列包含EditText / Spinner / DateWidget等('values')。即使我已经为android:layout_width="fill_parent"TableLayout和所有小部件指定了TableRow(在'values'列中)。

创建活动时,屏幕看起来很完美。但是,当在EditText中键入一个非常长的值时,“值”列超出了可见的屏幕区域。

我该如何解决这个问题?

android layout width tablelayout
6个回答
58
投票

您可能希望使用权重来定义列的大小。因此,您将定义表格布局高度以填充父级,但对于每列,您应将宽度设置为“0px”,将权重设置为希望列跨越的百分比。因此,假设您希望第一列为屏幕宽度的30%,则将其权重设置为“0.3”,将第二列设置为“0.7”。

试一试,看看是否有效。


24
投票

解决这个问题的实用方法是在stretchColumns中使用shrinkColumnsTableLayout属性。它们的值应该是基于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>

7
投票

这对我有用..

tableLayout.setColumnShrinkable(1,true);

5
投票
<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>

这段代码可以让我在第一列上对齐文本视图并编辑文本填充父级。


0
投票

试试这个,确保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>

0
投票

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"
       />
© www.soinside.com 2019 - 2024. All rights reserved.