不能强制edittext宽度跨越屏幕

时间:2016-07-20 11:34:22

标签: android layout android-edittext tablelayout android-tablelayout

在下面的布局文件中,我创建了一个scrollView,它包含一个tableLayout。 tablelayout中的第一行是Edittext。尽管我设置了Edittext视图参数 如下:

android:layout_width="match_parent"
android:layout_height="wrap_content"

它永远不会从左到右跨越屏幕!!

请让我知道如何使Edittext跨越屏幕从左到右?

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<!-- scrollView can host only one direct parent-->
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/tl1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TableRow
            android:id="@+id/tr0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <EditText
                android:id="@+id/et0"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </TableRow>
        <TableRow
            android:id="@+id/tr1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="3"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/rl_label_x1"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/tv_label_x1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="x1: "/>
                <TextView
                    android:id="@+id/tv_x1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toEndOf="@id/tv_label_x1"
                    android:text="x1value" />
            </RelativeLayout>
        </TableRow>
    </TableLayout>
</ScrollView>
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

我找到了。我应该将Edittext视图包装在Relativelayout中作为示例,如下所示:

            <RelativeLayout
                android:id="@+id/rl_et"
                android:layout_weight="1">
            <EditText
                android:id="@+id/et0"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            </RelativeLayout>

但是我仍然不知道为什么没有将Edittext包装在主机布局中它不起作用?? !!

答案 1 :(得分:0)

stretchColumns

TableLayout属性可能有所帮助。 像这样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <!-- scrollView can host only one direct parent-->
    <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TableLayout
                android:id="@+id/tl1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:stretchColumns="0">

            <TableRow
                    android:id="@+id/tr0"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                <EditText
                        android:id="@+id/et0"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"/>
            </TableRow>
            <TableRow
                    android:id="@+id/tr1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="3"
                    android:orientation="vertical">

                <RelativeLayout
                        android:id="@+id/rl_label_x1"
                        android:layout_weight="1">
                    <TextView
                            android:id="@+id/tv_label_x1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="x1: "/>
                    <TextView
                            android:id="@+id/tv_x1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_toEndOf="@id/tv_label_x1"
                            android:text="x1value"/>
                </RelativeLayout>
            </TableRow>
        </TableLayout>
    </ScrollView>
</RelativeLayout>

enter image description here