【问题标题】:How do I layout two EditTexts side by side to each other in a GridLayout?如何在 GridLayout 中并排布置两个 EditText?
【发布时间】:2015-08-24 23:49:11
【问题描述】:

我有两个 EditText。我希望每个都占据 UI 屏幕宽度的一半。

下面是我的 xml 布局文件。只有最左边的 EditText(“截止日期”)显示,所以我在这里清除遗漏的东西。应该显示的最右边的 EditText 是“到期时间”。请指教。

....
<android.support.design.widget.TextInputLayout
    android:id="@+id/DueDate_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_row="4"
    android:layout_column="0">

<com.example.jdw.thirdscreen.ListenerEditText
    android:id="@+id/FEditText"
    android:hint="Due Date"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="start"
    android:inputType="text|textCapSentences|textNoSuggestions"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF"
    android:singleLine="true"
    android:maxLength="51"
    android:imeOptions="actionNext|flagNoExtractUi"
    android:layout_marginBottom="8dp"
    android:nextFocusRight="@+id/DueTime_text_input_layout" >

</com.example.jdw.thirdscreen.ListenerEditText>
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:id="@+id/DueTime_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_row="4"
    android:layout_column="1">

<com.example.jdw.thirdscreen.ListenerEditText
    android:id="@+id/GEditText"
    android:hint="Due Time"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="start"
    android:inputType="text|textCapSentences|textNoSuggestions"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FFFFFF"
    android:singleLine="true"
    android:maxLength="51"
    android:imeOptions="actionNext|flagNoExtractUi"
    android:layout_marginBottom="8dp"
    android:nextFocusDown="@+id/saveButton" >

</com.example.jdw.thirdscreen.ListenerEditText>
</android.support.design.widget.TextInputLayout>
...

【问题讨论】:

    标签: xml android-layout android-edittext layout-gravity


    【解决方案1】:

    使用 weight 属性,自 API 21 起可用

    <GridLayout 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:orientation="horizontal"
        tools:context=".MainActivity"
        android:rowCount="4"
        android:columnCount="2">
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_gravity="fill_horizontal"
            android:layout_columnWeight="1" />
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_gravity="fill_horizontal"
            android:layout_columnWeight="1" />
    </GridLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-05-03
      • 1970-01-01
      • 2015-08-30
      • 2011-08-13
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2016-01-07
      相关资源
      最近更新 更多