[英]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
那个问题,这是解决方案:
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"
EditText
, LinearLayout
set android:weightSum=2.5"
(1 + 1.5) EditText
父视图中, LinearLayout
设置android:weightSum=2.5"
(1 + 1.5) 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 using TableLAyout instead 尝试改用TableLAyout
http://www.mkyong.com/android/android-tablelayout-example/ http://www.mkyong.com/android/android-tablelayout-example/
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.