2014-02-12 115 views
0

我試圖在水平線性佈局中將一個文本視圖設置爲左側和右側,這是嵌套在嵌套在scrollview中的垂直線性佈局中。水平線性佈局看起來是這樣的:在線性佈局中左對齊

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
<TextView android:id="@+id/txtType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="Type of Event: " 
    style="@style/black_text_bold" 
    android:visibility="gone"/> 
<TextView android:id="@+id/outputType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="Loading..." 
    style="@style/black_text" 
    android:visibility="gone"/>  
</LinearLayout> 

我該如何去對齊它?我想txttype開始在左邊和輸出類型結束對像正確的:

|       | 
| txtType:  outputType | 
|       | 

謝謝你在前進,

泰勒

+0

在textviews中使用layou_weights。 – njzk2

回答

1

使用這個..

<LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
     > 
    <TextView android:id="@+id/txtType" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:layout_weight="1" 
     android:text="Type of Event: " 
     style="@style/black_text_bold" 
     android:visibility="gone"/> 

    <TextView android:id="@+id/outputType" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:text="Loading..." 
     android:layout_weight="1" 
     style="@style/black_text" 
     android:visibility="gone"/>  
     </LinearLayout> 
1

可以使用layout_gravity屬性設置怎樣一個孩子應該置於其容器中。

您的左側視圖的layout_gravity應該是left,以將其與容器的左邊緣對齊。對於正確的視圖 - layout_gravityright同樣對齊到容器的右邊緣。

2

你可以只使用一個RelativeLayout和使用屬性alignParentLeftalignParentRight這樣的:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
<TextView android:id="@+id/txtType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginTop="10dip" 
    android:text="Type of Event: " 
    style="@style/black_text_bold" 
    android:visibility="gone"/> 
<TextView android:id="@+id/outputType" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dip" 
    android:text="Loading..." 
    android:layout_alignParentRight="true" 
    style="@style/black_text" 
    android:visibility="gone"/>  
</RelativeLayout> 
1
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView android:id="@+id/txtType" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:layout_width="1" 
     android:text="Type of Event: " 
     style="@style/black_text_bold" 
     android:visibility="gone"/> 

    <TextView android:id="@+id/outputType" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:layout_width="1" 
     android:text="Loading..." 
     style="@style/black_text" 
     android:visibility="gone"/>  
</LinearLayout> 
1

的TextView XML與ID 機器人:id =「@ + id/outputType」,change ** android:layout_width =「wrap_c意圖「**與android:layout_width =」0dip「並設置android:layout_weight =」1「。這些更改將幫助您獲得結果。

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

    <TextView 
     android:id="@+id/txtType" 
     style="@style/black_text_bold" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:text="Type of Event: " 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/outputType" 
     style="@style/black_text" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dip" 
     android:layout_weight="1" 
     android:gravity="right" 
     android:text="Loading..." 
     android:visibility="gone" /> 

</LinearLayout>