2017-06-13 76 views
1

我想在我的活動頂部佈局4個視圖(2個按鈕,2個TextViews)。我需要button_2以水平居中,TextView_2佔據視圖右側的剩餘空間,button_1以wrap_content的形式固定在父級左側,textView_1填充button_1和button_2之間的空間。 mock diagram to show what I mean如何佈置4視圖並排1視圖中心

我迄今爲止代碼:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
<Button 
    android:id="@+id/button_1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 
<TextView 
    android:id="@+id/textView_1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 
<Button 
    android:id="@+id/button_2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:layout_weight="1"/> 
<TextView 
    android:id="@+id/textView_2" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 
</LinearLayout> 

回答

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

<Button 
    android:id="@+id/button_1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:text="button_1"/> 
<TextView 
    android:id="@+id/textView_1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_toRightOf="@+id/button_1" 
    android:layout_toLeftOf="@+id/button_2" 
    android:layout_alignBottom="@+id/button_2" 
    android:text="textView_1"/> 
<Button 
    android:id="@+id/button_2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="button_2"/> 
<TextView 
    android:id="@+id/textView_2" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="textView_2" 
    android:gravity="center" 
    android:layout_alignBottom="@+id/button_2" 
    android:layout_alignParentEnd="true" 
    android:layout_toEndOf="@+id/button_2" /> 

    </RelativeLayout> 
+0

謝謝。我顯然走錯了嘗試使用線性佈局的路徑! –

+0

一個註釋,相對佈局高度需要更改爲wrap_content或覆蓋整個屏幕。 –

+0

@FredWhite謝謝,我忘記了。 – Anmol

0

試着在孩子的線性佈局和高度此

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="1"> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="1"> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"/> 
</LinearLayout> 
</LinearLayout> 

刪除佈局重量是改變包裝的TextView的和按鈕的內容fulfull您的要求

+0

與其他2個答案相似的佈局似乎是解決問題的正確方法。謝謝你的時間。 –

+0

與我的快樂 – Raj

1

Relative Layout將完全適合您的條件。明智地使用android:layout_toRightOf,android:layout_toLeftOf,android:layout_alignParentRightandroid:layout_alignParentLeft屬性。

<RelativeLayout 
     android:layout_width="match_parent" android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Button 
      android:id="@+id/button_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true"/> 
     <TextView 
      android:id="@+id/textView_1" 
      android:layout_toRightOf="@+id/button_1" 
      android:layout_toleftOf="@+id/button_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="hello there"/> 
     <Button 
      android:id="@+id/button_2" 
      android:layout_width="wrap_content" 
      android:layout_toRightOf="@+id/textView_1" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal"/> 
     <TextView 
      android:id="@+id/textView_2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/button_2" 
      android:layout_alignParentRight="true"/> 
    </RelativeLayout> 
+0

謝謝,我相信這與anmol的答案有相同的結果。我錯誤地嘗試使用線性佈局! –

+0

是的,當你需要更復雜的路線時,相對佈局要好得多。謝謝 –