2017-03-07 115 views
0

我有一個TextView。我想做所有的TextView(No.123 ... & Sea View ...)左對齊。我知道android:gravity="left"android:layout_gravity="left"但它不適合我的問題。如何對齊android中的所有TextView?

這是我的xml代碼;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:card_view="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Address " /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

     </LinearLayout> 

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

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Type "/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sea View, Graden View, Local" /> 

     </LinearLayout> 
</LinearLayout> 

如何使用align left text view?如下圖所示。

enter image description here

輸出結果;

enter image description here

+0

什麼是您的輸出?你能分享屏幕截圖嗎?並請更新代碼片段 –

+1

中的LinearLayout的父佈局,以某種方式顯示您想要移動的內容以及以何種方式。不清楚。 –

+0

見下文http://stackoverflow.com/a/42641608/3513479 沒有必要改變你的代碼只需把一條線 –

回答

2

使用相對佈局來實現你想要的,

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:id="@+id/address" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Address " /> 

     <TextView android:id="@+id/type" 
      android:layout_below="@+id/address" 
      android:layout_alignRight="@+id/address" 
      android:layout_width="match_parent" 
      android:gravity="left" 
      android:layout_height="wrap_content" 
      android:text="Type "/> 

     <TextView 
      android:layout_toRightOf="@+id/address" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

     <TextView 
      android:layout_alignBottom="@+id/type" 
      android:layout_toRightOf="@+id/type" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Sea View, Graden View, Local" /> 
    </RelativeLayout> 

enter image description here

+0

哦!我忘記使用RelativeLayout。謝謝。 – WPG

0

隨着layout_weight和weighSum的幫助下,你可以做到這一點,下面的代碼片段可以幫助你

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="2" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.2" 
      android:text="Address" /> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="1.8" 
      android:layout_height="wrap_content" 
      android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

    </LinearLayout> 

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

     <TextView 
      android:layout_width="0dp" 
      android:layout_weight="0.2" 
      android:layout_height="wrap_content" 
      android:text="Type"/> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1.8" 
      android:text="Sea View, Graden View, Local" /> 

    </LinearLayout> 

</LinearLayout> 

有關的LinearLayout和它的屬性更多的瞭解,你可以參考 https://developer.android.com/reference/android/widget/LinearLayout.html

0

使用layout_weight和LinearLayout中的weightSum財產,這將使你的佈局正確對齊和響應了。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Address" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="4" 
     android:layout_height="wrap_content" 
     android:text="No.123, Blah Street, Blah Township, Blah Country" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="5" 
    android:layout_marginTop="16dp" 
    android:orientation="horizontal"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Type"/> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="4" 
     android:text="Sea View, Graden View, Local" /> 

</LinearLayout> 

1

沒有做更多的在你的代碼只需添加一行 要將地址&類型

<TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Address" /> 

,而不是

<TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:ems="5" 
     android:text="Address" /> 

刪除安卓layout_weight = 「1」

可以增加減少EMS值按您的要求

現在你說的話是EMS

EMS指到XML佈局文件到這是我的固定寬度基於文本大小:

如果您設置了android:ems =「5」,那麼它會消耗5個字符

爲您設計的空間。 這意味着間接您可以設置您的佈局或TextText的寬度。

享受編碼;)