2011-11-25 175 views
2

我可以知道如何將這三個Textviews旁邊的Button對齊嗎?而不是在Textviews下方。 這是XML文件。 如何對齊右側的按鈕?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:orientation="vertical" android:id="@+id/linearlayout1" 
     android:background="#000000"> 

      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:id="@+id/textview1" 
       android:layout_alignParentLeft="true" android:text="Textview1" /> 

      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:text="Textview2" 
       android:layout_alignParentLeft="true" android:id="@+id/Textview2" /> 

      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
       android:text="Textview3" android:id="@+id/Textview3" /> 

     <RelativeLayout android:id="@+id/relativeLayout2" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <Button android:id="@+id/button" android:layout_width="70px" 
       android:layout_height="38px" android:text="Button" 
       /> 
     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout> 

回答

5

IMO你應該把三個textviews和按鈕,在RelativeLayout的,並擺脫的LinearLayout的。因爲每個ViewGroup都會在佈局和繪製過程中增加開銷,所以您只能使用一個佈局時沒有意義。做:

<RelativeLayout> 
    <TextView android:id="@+id/tv1" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"/> 
    <TextView android:id="@+id/tv2" android:layout_alignParentLeft="true" android:layout_below="@id/tv1"/> 
    <TextView android:id="@+id/tv3" android:layout_alignParentLeft="true" android:layout_below="@id/tv2"/> 
    <Button android:id="@+id/btn" android:layout_toRightOf="@id/tv1"/> 
</RelativeLayout> 

我沒有編譯這個,但它應該做你所需要的。玩弄你給出的佈局參數按鈕(你可以將它對齊到父項的頂部和右側,或任何文本框的右側等)。

的RelativeLayout的很厲害;你應該真正利用它的靈活性。

+0

我將按鈕設置在tv3的右側。那麼tv1和tv2會相互覆蓋。 – sowhat

3

使用attribut您Buttonandroid:layout_alignParentRight = "true"

所以你Button XML代碼將是這樣的:

<Button android:id="@+id/button" 
     android:layout_width="70px" 
     android:layout_height="38px" 
     android:text="Button" 
     android:layout_alignParentRight="true" <!-- align the Button on the right side --> 
/> 
+0

那不是隻是把三個文本框下面的按鈕,排列在屏幕的右側? – noobler

+0

可以對齊按鈕,使其位於文本視圖的「中心」右側?因爲我有三個文字瀏覽,現在我使用alignParentRight後,該按鈕在底部,它仍然在底部,但對齊到正確的位置。 – sowhat

+0

@noobler:三個textViews的推杆上的LinearLayout,用戶傢伙想按鈕對準右側 – Houcine

-1

你的問題標題建議你只是想對齊該按鈕位於屏幕的右側。在這種情況下,其他答案很好。不過,在你的問題中,你說你想讓按鈕在Textviews旁邊。在這種情況下,閱讀以下內容:

你的外LinearLayout是垂直方向,所以其所有直接子女將低於對方。根據你想要的外觀,你可能想在你目前的LinearLayout之外或之內嵌入一個水平的LinearLayout

一個RelativeLayout定位其子彼此相對,而不是相對於東西在它之外。也許你想沿着這些線路的東西(我沒有嘗試進行編譯):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" android:layout_height="fill_parent" 
     android:orientation="horizontal" android:id="@+id/linearlayout1" 
     android:background="#000000"> 

    <Button android:id="@+id/button" android:layout_width="70px" 
     android:layout_height="38px" android:text="Button" /> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" android:layout_height="fill_parent" 
      android:orientation="vertical" android:id="@+id/linearlayout1" 
      android:background="#000000"> 

      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:id="@+id/textview1" 
       android:layout_alignParentLeft="true" android:text="Textview1" /> 

      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:text="Textview2" 
       android:layout_alignParentLeft="true" android:id="@+id/Textview2" /> 

      <TextView android:layout_width="wrap_content" 
       android:layout_height="wrap_content" android:layout_alignParentLeft="true" 
       android:text="Textview3" android:id="@+id/Textview3" /> 
    </LinearLayout> 
</LinearLayout> 
0

您還可以使用:

android:layout_gravity="end"