2012-01-04 16 views
8

Tôi đặt 4 nút trong bố cục tương đối. Tôi muốn chúng có cùng chiều rộng và sửa cho bất kỳ kích thước màn hình nào của điện thoại. Mã của tôi như sau:Làm cho các nút có cùng chiều rộng trong bố cục

<RelativeLayout 
      android:id="@+id/relativeLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="38dp" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/button1" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/button2" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/button3" 
       android:text="Button" /> 

     </RelativeLayout> 

Tôi nên sửa đổi điều gì?

Trả lời

15

Chuyển sang LinearLayout và đặt tất cả các nút bằng trọng lượng.

ví dụ:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="38dp" 
    android:orientation="horizontal"> 
    <Button android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 
    <Button android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 
    <Button android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" /> 
</LinearLayout> 
+0

Có giải pháp nào trong RelativeLayout? – inexcii

+0

Lúc đó tôi viết câu trả lời không có cách nào khác. –

+4

Được rồi, nhưng bây giờ thì sao? – inexcii

Các vấn đề liên quan