【问题标题】:Android: horizontal LinearLayout does not lay out elements as expectedAndroid:水平LinearLayout没有按预期布置元素
【发布时间】:2020-06-09 03:38:03
【问题描述】:

我创建了一个包含 EditText 和一个按钮的水平 LinearLayout。按钮应尽可能窄,EditText 应占据整个剩余空间。 像这样:

我写了这段代码:

<LinearLayout
    android:id="@+id/textInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/editTextToSpeak"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ems="10"
        android:hint="@string/text_to_speak"
        android:inputType="text" />

    <Button
        android:id="@+id/saveCard"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/save_card" />
</LinearLayout>

我得到了这张照片:

按钮完全不可见,EditText的高度增加了一倍。

如何让这些元素看起来像第一张图片?

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    您可以通过在 EditText 中使用 android:layout_weight="1"android:layout_width = "0dp" 轻松归档它,因此它将占用除按钮区域之外的其余空间。

    <LinearLayout
        android:id = "@+id/textInputLayout"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:orientation = "horizontal">
    
        <EditText
            android:layout_weight="1"
            android:id="@+id/editTextToSpeak"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:ems="10"
            android:hint="@string/text_to_speak"
            android:inputType="text" />
    
        <Button
            android:id="@+id/saveCard"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/save_card" />
    </LinearLayout>
    

    希望有所帮助:)

    【讨论】:

      【解决方案2】:

      你也可以在android中使用Relative布局来实现。

      <?xml version="1.0" encoding="utf-8"?>
      
      <RelativeLayout android:layout_height="wrap_content"
          android:layout_width="match_parent"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      
           <EditText
              android:id="@+id/editTextToSpeak"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:ems="10"
              android:hint="@string/text_to_speak"
              android:inputType="text"
              android:layout_toStartOf="@+id/saveCard"/>
      
           <Button
             android:id="@+id/saveCard"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/save_card"
             android:layout_alignParentEnd="true"/>
      </RelativeLayout>
      

      希望对你有帮助。)

      【讨论】:

        【解决方案3】:

        使用 android:weightSum & android:weight。它将使您能够垂直或水平固定尺寸。

        <LinearLayout
        android:id = "@ + id/textInputLayout"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:orientation = "horizontal"
        android:weightSum="3">
        
        <EditText
            android:id = "@+id/editTextToSpeak"
            android:layout_width ="match_parent"
            android:layout_height = "match_parent"
            android:ems = "10"
            android:hint = "@string/text_to_speak"
            android:inputType = "text"
             />
        
        <Button
            android:id = "@+id/saveCard"
            android:layout_width = "wrap_content"
            android:layout_height = "match_parent"
            android:text = "@ string/save_card" 
            android:layout_weight="1"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-10
          • 2020-02-17
          • 1970-01-01
          • 2019-06-30
          相关资源
          最近更新 更多