2012-03-23 32 views
9

Vì vậy, tôi có bố trí này:android "layout_alignParentBottom" trong cách bố trí tương đối

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#FFFF00" 
       android:minHeight="100dp" 
       android:layout_gravity="bottom" 
     > 
    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:textColor="#000000" 
      android:background="#FF0000" 
      android:text="Hello World" 
      /> 

    <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="button"/> 
</RelativeLayout> 

và đây là cách có vẻ:

enter image description here

nhưng nếu tôi thêm android:layout_alignParentBottom="true" vào nút ở đây là trông như thế nào:

enter image description here

  1. Ai đó có thể giải thích cho tôi hành vi này không?
  2. Làm thế nào để đặt nút của tôi ở dưới cùng mà không thay đổi kích thước bố cục màu vàng và không thêm hàng nghìn bố cục cho giải pháp?
+0

Lỗi này vẫn tồn tại? –

+0

Tôi đã gặp sự cố này trên Android 2.2 và 4.0.0 nhưng tôi nghĩ điều này cũng xảy ra trên các nền tảng khác quá –

Trả lời

9

giải pháp này làm việc cho tôi

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/layout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="#FFFF00" 
       android:minHeight="100dp" 
       android:orientation="horizontal" 
       android:layout_gravity="bottom" 
     > 
    <TextView 
      android:id="@+id/tv" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:textColor="#000000" 
      android:background="#FF0000" 
      android:text="Hello World" 
      /> 

    <Button 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_gravity="bottom|right" 
      android:text="button"/> 
</FrameLayout> 
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom" 
    android:background="#FFFF00" 
    android:minHeight="100dp" > 

    <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:textColor="#000000" 
      android:background="#FF0000" 
      android:text="Hello World"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="button" /> 

</RelativeLayout> 

thử mã này

+0

bằng mã của bạn để bố cục của tôi bao phủ tất cả màn hình vì bạn đã sử dụng android: layout_height = "fill_parent" và tôi muốn bố cục của tôi giữ nguyên chiều cao như trong ảnh chụp màn hình đầu tiên của tôi –

+0

có một mã u nào không? – Android

+0

vì vậy tôi muốn bố cục của tôi trông giống như hình ảnh đầu tiên của tôi, ngoại trừ nút của tôi phải được căn chỉnh ở dưới cùng. –

1

một một màn hình nhanh chóng này

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="bottom" 
    android:background="#FFFF00" 
    android:minHeight="100dp" > 



    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:text="button" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignTop="@+id/button1" 
     android:background="#000000" 
     android:text="TextView" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_alignParentLeft="true" 
     android:background="#000000" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical" > 

    </LinearLayout> 

</RelativeLayout> 
+0

điều này không hoạt động vì nó không tính đến minHeight từ bố cục tương đối chính –

1

chỉ cho thử, bạn có thể tăng gấp đôi kích thước tối thiểu bố cục của bạn và thử lại không? hoặc có thể bạn có thể đặt sửa chữa cao độ của bố cục và thay đổi nó động từ mã khi cần.

0

Rất tiếc, lỗi vẫn tồn tại trong các phiên bản Android mới hơn.

Dù sao, tôi thấy rằng sau đây có thể giải quyết vấn đề đó:

  1. Xóa minheight của RelativeLayout

  2. Chèn TextView trong một LinearLayout:

    <LinearLayout 
        android:id="@+id/tvLayout" 
        android:layout_alignParentTop="true" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" 
        android:minHeight="100dp" 
        android:gravity="top" 
        > 
        <TextView 
         android:id="@+id/tv" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:background="#FFFF00FF" 
         android:text="Helo" 
         /> 
    </LinearLayout> 
    
  3. Xóa layout_alignParentBottom từ Button và thêm layout_alignBottom = "@ id/tvLayout"

Bây giờ LinearLayout "kiểm soát" chiều cao của RelativeLayout. Nếu chiều cao TextView lớn hơn minHeight 100dp, nó sẽ mở rộng.

Và nút sẽ luôn căn chỉnh đáy của nó thành LinearLayout, bằng với RelativeLayout.

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