簡體   English   中英

在LinearLayout(水平)中將textview左對齊

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM