简体   繁体   English

Android TableLayout宽度

[英]Android TableLayout Width

I have a two column TableLayout as the only child of a scroll view. 我有两列TableLayout作为滚动视图的唯一子级。 The first column contains TextViews ('labels') and the second column contains EditText / Spinner / DateWidget etc ('values'). 第一列包含TextViews (“标签”),第二列包含EditText / Spinner / DateWidget等(“值”)。 Even though I have have specified android:layout_width="fill_parent" for TableLayout , TableRow & all widgets (in 'values' column). 即使我已经为TableLayoutTableRow和所有小部件(在“值”列中)指定了android:layout_width="fill_parent"

The screen looks perfect when the activity is created. 创建活动后,屏幕看起来很完美。 However, when one types a really long value in the EditText , the 'values' column goes beyond the visible screen area. 但是,当在EditText键入一个非常长的值时,“值”列将超出可见的屏幕区域。

How do I tackle this? 我该如何解决?

You may want to define the sizes of the columns by using a weight. 您可能需要使用权重来定义列的大小。 So you will define the table layout height to fill parent but for each column you should set the width to "0px" and the weight to the percentage you want the column to span. 因此,您将定义表格布局高度以填充父级,但是对于每列,您都应将宽度设置为“ 0px”,权重设置为您希望列跨越的百分比。 So assuming you want the first column to be 30% of the screen width you set it's weight to "0.3" and the second column to "0.7". 因此,假设您希望第一列为屏幕宽度的30%,则将其权重设置为“ 0.3”,将第二列设置为“ 0.7”。

Try it and see if that works. 试试看,看看是否可行。

The pragmatic way to solve this is to use the stretchColumns and shrinkColumns attributes in TableLayout . 解决此问题的实用方法是使用TableLayoutstretchColumnsshrinkColumns属性。 Their values should be 0-based indexes of columns. 它们的值应为基于0的列索引。

For example, if you have a two column Table layout and: 例如,如果您有两列表格布局,并且:

  • You would like the second column to fill up with empty space (stretch) so the TableLayout fits the entire parent view on a large screen device 您希望第二列填充空白空间(拉伸),以便TableLayout适合大屏幕设备上的整个父视图
  • You would like the first column to be shrunk (and its contents possibly wrapped / ellipsized) on a small screen device 您希望将第一列缩小(并且其内容可能会被包裹/省略)在小屏幕设备上

you would define your TableLayout as: 您可以将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>

This code worked for me to align text view on first column and edit text fill parent. 这段代码对我有用,可以使第一列上的文本视图对齐并编辑父级文本填充。

Try this, make sure android:singleLine="false" and android:inputType="textMultiLine" : 试试这个,确保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>

I had similar problem with EditText on Android 2.2. 我在Android 2.2上使用EditText遇到类似的问题。 In my case adding android:maxWidth="0sp" property helped. 就我而言,添加android:maxWidth =“ 0sp”属性很有帮助。 Now EditText field is displayed as I wanted - so it is still of the same size as other EditText fields, but additionaly it is not resized when long text is entered. 现在,EditText字段将按我的意愿显示-因此它的大小仍然与其他EditText字段相同,但是另外,当输入长文本时,它不会调整大小。

Here is my EditText definition: 这是我的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