![](/img/trans.png)
[英]How to align vertical textview and editText in Linearlayout in android
[英]Align a textview to left in a LinearLayout(horizontal)
我試圖使我的Textview
左對齊嘗試。 和android:gravity="left"
但是什么也沒發生。
這是我的XML代碼:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/bg"
android:hint="Enter Address"/>
</LinearLayout>
試試這個使用代碼:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView11"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toEndOf="@+id/textView11"
android:layout_weight="1"
android:background="@drawable/bg"
android:hint="Enter Address"/></RelativeLayout>
gravity
屬性用於設置View
內容的重力。
layout_gravity
屬性用於設置視圖自身相對於其父視圖的重力。
因此,如果您想在TextView
設置文本的重力,則可以使用gravity
。 如果要在LinearLayout
設置TextView
的重力,則應使用layout_gravity
。
嘗試使用由LinearLayout
android:layout_alignParentLeft="true"
RelativeLayout
並讓您的TextView
android:layout_alignParentLeft="true"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:text="Email:"
android:textSize="20dp"/>
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:background="@drawable/bg"
android:hint="Enter Address"/>
</RelativeLayout>
如果要使用線性布局,請使用:
<TextView
android:id="@+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentLeft="true"
android:text="Email:"
android:textSize="20dp"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.