【问题标题】:How to put this EditText on the right in a LinearLayout?如何将此 EditText 放在 LinearLayout 的右侧?
【发布时间】:2015-03-29 18:45:48
【问题描述】:

我创建了这个表单:

编写此 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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" >

    <LinearLayout
        android:id="@+id/fields"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp">

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

            <TextView
                android:id="@+id/textCountry"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_country"
                android:textColor="@color/black" />

            <Spinner
                android:id="@+id/spinnerCountry"
                android:layout_height="wrap_content"
                android:layout_width="0dp"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editCustom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/city"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_city"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editCity"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
         </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/postal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_postal"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editPostal"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textAddress"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_address"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editAddress"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textTel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/text_tel"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editTel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="text"
                android:textColor="@color/black" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textEmail"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="18sp"
                android:text="@string/email"
                android:textColor="@color/black" />

            <EditText
                android:id="@+id/editEmail"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:textSize="18sp"
                android:inputType="textEmailAddress"
                android:textColor="@color/black" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

如您所见,第二个 EditText 太长,我会将其与其他字段对齐,如下所示:

我尝试在那里使用RelativeLayout 并使用LinearLayout 进行一些更改,但我快疯了,因为我无法达到我的目的。 我对 XML 布局不是那么专业... :(

【问题讨论】:

  • 一个快速的hack,就是添加一个TextView,就像你对其他人所做的一样,并设置没有文本的TextView。或任何观点。
  • 我是这样做的,但我正在寻找更好的解决方案:)
  • @smartmouse 如果您对 Carnal 提供的解决方案不满意,那么我认为最好使用表格布局来设计此 UI
  • 我解决了 Phonbopit 发布的解决方案。还是谢谢你。

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


【解决方案1】:

我只编辑了第二个LinearLayout那个问题,这是解决方案:

  1. EditText 中,您必须将layout_weight="1.5" 设置为与其他和android:layout_width="0dp" 相同
  2. EditTextLinearLayout 的父视图中设置android:weightSum=2.5" (1 + 1.5)
  3. android:gravity="right" 设置为LinearLayout Fin!

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2.5"
        android:gravity="right"
        android:orientation="horizontal">
    
        <EditText
            android:id="@+id/editCustom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:inputType="text"
            android:layout_weight="1.5"
            android:textColor="@color/black" />
    </LinearLayout>
    

【讨论】:

  • 这很酷!你能解释一下 weightSum 1+1,5 的行为吗?
  • @smartmouse 在另一个 LinearLayout 上,您有 TextView,layout_weight = 1 和 EditText layout_weight = 1.5,总共 2.5,您可以阅读这个比我解释得更好stackoverflow.com/questions/7452741/…
【解决方案2】:

通过

替换您的第二个 EditText 代码
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
        <TextView
            android:id="@+id/textCountry"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="" />
        <EditText
            android:id="@+id/editCustom"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:inputType="text"
            android:textColor="@color/black"
            android:textSize="18sp" />
    </LinearLayout>

您可以添加空的 Textview 并通过为两者定义适当的权重来实现它。并确保在为任何视图定义 layout_weight 时将宽度定义为 android:layout_width="0dp"

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

      尝试正确设置重力,然后设置一些边距。可能会做你想做的事。大多数时候我更喜欢使用RelativeLayouts,因为它们更容易操作。

      【讨论】:

        【解决方案5】:

        在你的 EditText 的父 LinearLayout 视图中有这个:

        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:alignLeft="@id/spinnerCountry"
                android:orientation="horizontal">
        

        这会将整个 LinearLayout 的左侧与 Spinner 的左侧对齐。

        【讨论】:

        • 你的意思是android:layout_alignLeft="@id/spinnerCountry?在这种情况下,我收到此错误:Invalid layout param in a LinearLayout: layout_alignLeft
        • 抱歉,我将滚动视图中的布局误读为相对而非线性。那么你得到的错误是有道理的
        猜你喜欢
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 2021-01-21
        • 2014-04-29
        • 1970-01-01
        • 1970-01-01
        • 2011-12-10
        • 1970-01-01
        相关资源
        最近更新 更多