简体   繁体   English

如何将此EditText放置在LinearLayout的右侧?

[英]How to put this EditText on the right in a LinearLayout?

i created this form: 我创建了这种形式:

在此处输入图片说明

writing this XML layout: 编写此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>

As you can see the second EditText is too long and i would align it to the other fields, like this: 如您所见,第二个EditText太长,我会将其与其他字段对齐,如下所示:

在此处输入图片说明

I tried to use there a RelativeLayout and do some changes with LinearLayout but i'm getting crazy because i can't reach my purpose. 我试图在那里使用RelativeLayout并使用LinearLayout进行一些更改,但是我发疯了,因为我无法达到我的目的。 I am not so expert about XML layouts... :( 我不是XML布局专家... :(

I edit only second LinearLayout that problem , here this is solution : 我只编辑第二个LinearLayout那个问题,这是解决方案:

  1. In EditText you must set layout_weight="1.5" as same as other and android:layout_width="0dp" EditText您必须将layout_weight="1.5"为与other和android:layout_width="0dp"
  2. In Parent View of EditText , LinearLayout set android:weightSum=2.5" (1 + 1.5) EditText父视图中, LinearLayout设置android:weightSum=2.5" (1 + 1.5)
  3. set android:gravity="right" to LinearLayout Fin! 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> 

replace your code for second EditText via 通过替换第二个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>

you can add empty Textview and by defining proper weight for both you achieve it. 您可以添加空的Textview并通过为两者实现适当的权重来实现。 And make sure when you define layout_weight for any view define width as android:layout_width="0dp" 并确保在为任何视图定义layout_weight时,将宽度定义为android:layout_width =“ 0dp”

Try setting gravity right and then some margins. 尝试正确设置重力,然后设置一些边距。 Might do what you want. 可能会做您想要的。 Most of the times I prefer using RelativeLayouts since they are easier to manipulate. 大多数时候,我更喜欢使用RelativeLayouts,因为它们更易于操作。

In your EditText's parent LinearLayout view have this: 在您的EditText的父LinearLayout视图中,具有以下内容:

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

This will align the entire LinearLayout's left hand side to the left hand side of the Spinner. 这会将整个LinearLayout的左侧与Spinner的左侧对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将动态创建的EditText和Spinner放在一个LinearLayout中? - How to put a dynamically created EditText and a Spinner in one LinearLayout? LinearLayout将孩子放在右侧 - LinearLayout put child at the right side 如何将 ProgressBar 放在 EditText 内并将栏保持在其中? - How to put ProgressBar inside of EditText and keep the bar right in it? 如何在Android中的线性布局的所需位置动态地向线性布局添加edittext - how to add edittext to a linearlayout dynamically at desired positions to the linearlayout in android 如何将listview和recyclerview放入linearlayout - How to Put listview and recyclerview in linearlayout 如何使EditText在可滚动的LinearLayout中可滚动? - How to make an EditText scrollable inside a scrollable LinearLayout? 如何在水平LinearLayout中垂直对齐EditText和ProgressBar? - How to vertically align EditText and ProgressBar in horizontal LinearLayout? 如何以编程方式将TextView和EditText对齐到水平LinearLayout中? - How to align the TextView and EditText into horizontal LinearLayout programmatically? 在LinearLayout中如何在按钮上放置EditText? - How to place EditText over a button when in LinearLayout? 如何在LinearLayout中向右移动微调器? - How to move spinner in LinearLayout to right?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM