繁体   English   中英

Android TableLayout宽度

[英]Android TableLayout Width

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

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

我该如何解决?

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

试试看,看看是否可行。

解决此问题的实用方法是使用TableLayoutstretchColumnsshrinkColumns属性。 它们的值应为基于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"
       />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM