【问题标题】:Android EditText and Button on same lineAndroid EditText 和 Button 在同一行
【发布时间】:2011-04-04 01:40:15
【问题描述】:

我需要在Search button 旁边有一个Edittext。 EditText 应该尽可能多地填充宽度,按钮应该在右边并且足够大以容纳它的文本。

现在看起来像这样:

[EDIT TEXT][Search]

但应该是这样的:

[.......EDIT TEXT.......][Search]

这里是XML:

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <EditText android:id="@+id/search_box" 
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="type to filter"
            android:inputType="text"
            android:maxLines="1" />
        <Button android:id="@+id/search_text"
            android:layout_toRightOf="@id/search_box"
            android:text="Search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
  </RelativeLayout>

【问题讨论】:

标签: android layout android-edittext android-button


【解决方案1】:

必须是相对布局吗?
我建议如下:将EditText layout_width 设置为fill_parent 并将其layout_weight 设置为1,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent">
<EditText android:text="@+id/EditText01" 
    android:id="@+id/EditText01"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:layout_width="fill_parent">
</EditText>
<Button android:text="@+id/Button01" 
    android:id="@+id/Button01"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</Button>

</LinearLayout>

【讨论】:

  • 我也建议使用 LinearLayout 而不是 Relative。我认为您需要将 layout_width 指定为不是 wrap_content 或 fill_parent,而是 0dp 或其他一些可接受的最小宽度。
【解决方案2】:

在这种情况下,EditText 的宽度必须设置为 fill_parent(而不是 wrap_content):

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content">
        <EditText android:id="@+id/search_box" 
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="type to filter"
            android:inputType="text"
            android:maxLines="1" />
        <Button android:id="@+id/search_text"
            android:layout_toRightOf="@id/search_box"
            android:text="Search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
  </RelativeLayout>

编辑

正如你所说,它隐藏了按钮。然后,只需颠倒顺序:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/RelativeLayout01" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
        <Button android:id="@+id/search_text"
            android:layout_alignParentRight="true"
            android:text="Search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText android:id="@+id/search_box" 
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@id/search_text"
            android:layout_centerVertical="true"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="type to filter"
            android:inputType="text"
            android:maxLines="1" />
</RelativeLayout>

【讨论】:

    【解决方案3】:

    我相信 Cristian 的答案适用于更高版本的 SDK,但对于以前的版本,您需要先定义 Button,然后将 EditText 放置到 layout_toLeftOf="@id/search_box",由方式,两个对象应该有不同的 ID。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button
            android:id="@+id/search_button"
            android:text="Search"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            />
        <EditText
            android:id="@+id/search_text"
            android:layout_toLeftOf="@+id/search_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="type to filter"
            android:inputType="text"
            android:maxLines="1"
            />
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      试试这个我已经修复了一点。它显示编辑文本的按钮右侧,并允许增加编辑文本宽度。

      <?xml version="1.0" encoding="utf-8"?>
      
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
             android:padding="10dip">
      
          <TextView
              android:id="@+id/label"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="instructions"/>
          <EditText
              android:id="@+id/entry"
              android:layout_width="250dip"
              android:layout_height="wrap_content"
              android:background="@android:drawable/editbox_background"
              android:layout_below="@id/label"/>
      
          <Button
              android:id="@+id/ok"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
      
              android:layout_alignRight="@id/entry"
              android:layout_alignParentRight="true"
              android:layout_marginLeft="1dip"
              android:text="ok" />
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

        对于那些想要相对布局解决方案的人,试试这个。我的目标是保持按钮大小不变,但在手机旋转到横向模式或不同手机屏幕尺寸时调整 EditText 字段。

        所以诀窍是使用边距组合并将父级对齐到开头。这是你可以尝试的,我直接从我的应用程序中提取的。

        <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/mainBackground"
            android:orientation="vertical"
            android:paddingLeft="3dp"
            android:paddingRight="1dp"
            tools:context=".MainActivity">
        
            <EditText
                android:id="@+id/itemEntry"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentBottom="true"
                android:layout_marginStart="14dp"
                android:layout_marginEnd="100dp"
                android:layout_marginBottom="17dp"
                android:imeOptions="actionDone"
                android:inputType="text"
                android:textColor="@color/textColor" />
        
            <Button
                android:id="@+id/button_insert"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/itemEntry"
                android:layout_marginStart="9dp"
                android:layout_marginTop="-4dp"
                android:layout_marginEnd="9dp"
                android:layout_alignParentEnd="true"
                android:background="@drawable/round_button"
                android:text="+"
                android:textColor="#ffffff"
                android:textSize="20dp"
                android:layout_alignParentRight="true"/>
        </RelativeLayout>
        

        您将得到的是 EditText 的大小将根据屏幕宽度而变化,而按钮将在最后保持固定。由于android:layout_alignParentEnd="true",按钮卡在最后。为了阻止 EditText 超出 Button,使用了android:layout_marginEnd="100dp"。希望这会有所帮助!

        【讨论】:

        • 和其他帖子有什么区别?
        • 它使用RelativeLayout,我对它的工作原理提供了更多解释。
        【解决方案6】:

        现在更好的答案是将 EditText 的 layout_width 设置为 0dp

        <LinearLayout 
             android:layout_height="wrap_content" 
             android:orientation="horizontal"
             android:layout_width="fill_parent">
        <EditText
             android:layout_height="wrap_content" 
             android:layout_weight="1"
             android:layout_width="0dp"/>
         <Button
            android:text="Button"
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content"/>
         </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 2011-03-31
          • 2016-10-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-04
          相关资源
          最近更新 更多