2012-12-23 40 views
12

Tôi có 3 RelativeLayout nằm bên trong main_RelativeLayout. Chế độ xem sẽ theo chiều dọc. Vì vậy, tôi có 3 RelativeLayout mà setted bên cạnh nhau, và tôi muốn họ để điền vào toàn bộ màn hình, không có vấn đề gì sẽ là kích thước màn hình. Tôi, xem bố trí:RelativeLayout weight

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/backg" 
> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="@dimen/top_mr_image" 
    android:src="@drawable/temp" /> 

<RelativeLayout 
    android:id="@+id/r1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/imageView1" 
    android:layout_marginLeft="10dp" 
    android:background="@drawable/r1bg" 
    android:layout_weight="1" 

    > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/txt_mr_right" 
     android:layout_marginRight="@dimen/txt_mr_right" 
     android:layout_marginTop="39dp" 
     android:text="S" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@id/textView1" 
     android:layout_marginLeft="@dimen/txt_mr_right" 
     android:layout_marginRight="@dimen/txt_mr_right" 
     android:text="T" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
</RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/r2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/r1" 
     android:layout_toRightOf="@+id/r1" 
     android:layout_weight="1" > 
    </RelativeLayout> 

      <RelativeLayout 
     android:id="@+id/r3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignTop="@+id/r2" 
     android:layout_toRightOf="@+id/r2" 
     android:layout_weight="1" > 

    </RelativeLayout> 

tôi đặt weight=1layout_width=0dp cho mỗi releativeLayout và kỹ thuật này hoạt động với các nút, tôi nghĩ như vậy sẽ ở cùng RelativeLayout, dường như suy nghĩ của tôi đã sai. Bất kỳ ý tưởng?

UPD1: Tôi đã thêm một hình ảnh của những gì tôi muốn có

enter image description here

+0

tôi đã thực hiện những gì tôi đang tìm kiếm, nhờ cho ý kiến ​​ – Daler

Trả lời

20

RelativeLayout không chú ý đến android:layout_weight. (Đó là tài sản của LinearLayout.LayoutParams, nhưng không phải là RelativeLayout.LayoutParams.)

Bạn sẽ có thể có bố cục bạn muốn với hệ thống phân cấp chế độ xem đơn giản hơn nhiều. Nó không rõ ràng những gì bạn đang cố gắng để làm, kể từ khi hai cuối cùng RelativeLayout s là sản phẩm nào. Nếu bạn cần một tổ chức hoàn toàn theo chiều dọc, tôi khuyên bạn nên sử dụng LinearLayout thay vì RelativeLayout.

EDIT Dựa trên chỉnh sửa của bạn, có vẻ như bạn muốn bố cục nằm ngang của ba chế độ xem phức hợp, mỗi chế độ xem có thể nhấp. Tôi nghĩ rằng một cái gì đó như sau sẽ làm việc:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 

    <!-- First column --> 
    <LinearLayout 
     android:id="@+id/firstColumn" 
     android:orientation="vertical" 
     android:gravity="center_horizontal" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     > 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="..." /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:text="text 1" 
      . . . /> 
    </LinearLayout> 

    <!-- Second column --> 
    <LinearLayout . . . > 
     . . . 
    </LinearLayout> 
</LinearLayout> 

Nếu nội dung của các nút là không đúng, bạn có thể thay thế thứ hai cấp LinearLayout quan điểm với RelativeLayout nếu điều đó giúp tổ chức bố trí tốt hơn.

+0

Tôi có các phần tử bên trong relativeLayout phải được định vị theo cách chính xác, nó sẽ là loại phần tử clickalble tùy chỉnh với hình ảnh và v.v. Vì mã xác nhận của tôi kém – Daler

+0

@Daler - Nếu bạn cung cấp thêm chi tiết về bố cục bạn muốn (một bản phác thảo hoặc ảnh chụp màn hình sẽ rất tuyệt), chúng tôi có thể giúp bạn đạt được nó. Cũng lưu ý rằng một 'LinearLayout' dọc có thể chứa các bố cục lồng nhau để cung cấp tổ chức ngang trên bất kỳ hàng nào cần nó. –

+0

cảm ơn thông tin phản hồi của bạn, tôi đã cập nhật bài viết của tôi, bạn có thể xem những gì tôi muốn có. Tôi muốn làm cho mỗi relativeLayout có thể nhấp được, vì vậy chúng sẽ hoạt động như một nút. cả ba đều có cùng thiết kế với các loại hình ảnh và văn bản khác nhau – Daler

4

RelativeLayouts không hỗ trợ cân. Bạn cần sử dụng LinearLayout làm vùng chứa chính nếu bạn muốn sử dụng trọng số.

+0

gì nếu phụ huynh là RelativeLayout? và tôi có thể thêm phần còn lại relativeLayouts bên trong linearLayout. Một chút hardcore nhưng có thể – Daler

+0

Tôi đoán bạn có thể có một RelativeLayout -> LinearLayout -> RelativeLayout x 3, nhưng tôi không chắc chắn những gì sẽ thực hiện. – dmon

0

Giải pháp rất đơn giản. Tôi đã tìm kiếm phân bố trọng lượng trong bố cục tương đối.

Đó là một mẹo nhỏ cho tất cả các tình huống loại này.

Sử dụng LinearLayout với android: định hướng = "ngang"

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