【问题标题】:cant force the edittext width to span along the screen不能强制edittext宽度跨越屏幕
【发布时间】:2016-11-23 15:02:01
【问题描述】:

在下面的布局文件中,我创建了一个滚动视图,它包含一个 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>

【问题讨论】:

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


    【解决方案1】:

    我找到了。例如,我应该将 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 包装在主机布局中就无法工作??!!

    【讨论】:

      【解决方案2】:

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

      <?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>
      

      【讨论】:

      • 奇怪的是它适用于我的环境(模拟器,Android 6)
      • 可能是因为在我拥有的原始布局中,Edittext 视图下方有额外的三个表格行。它们类似于以下内容:x1:x1value x2:x2value x3:x3value
      猜你喜欢
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多