6

Tôi đang gặp sự cố thực sự cố gắng sắp xếp các đoạn bằng một số RelativeLayout, mặc dù có vẻ như điều này đơn giản. Tôi chỉ cần hai mảnh bên cạnh nhau và nếu tôi sử dụng một LinearLayout nó hoạt động tốt:Sắp xếp các mảnh bằng RelativeLayout

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

    <fragment android:name="com.fragment.test.TitlesFragment" 
      android:id="@+id/fragmentTitles" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
    /> 

    <FrameLayout 
      android:id="@+id/details" 
      android:layout_weight="1" 
      android:layout_width="0px" 
      android:layout_height="fill_parent" 
    /> 

</LinearLayout> 

enter image description here

Tuy nhiên, nếu tôi sử dụng một RelativeLayout, không có gì cho thấy:

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

    <fragment android:name="com.fragment.test.TitlesFragment" 
      android:id="@+id/fragmentTitles" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
    /> 

    <FrameLayout 
      android:id="@+id/details" 
      android:layout_weight="1" 
      android:layout_width="0px" 
      android:layout_height="fill_parent" 
      android:layout_toRightOf="@id/fragmentTitles"    
    /> 

</RelativeLayout> 

enter image description here

Cập nhật:

Đây là một ảnh chụp màn hình của những gì tôi đang nhìn thấy:

enter image description here

Đây là mã Tôi đang sử dụng:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent" 
     android:weightSum="3" 
    > 
     <fragment android:name="com.fragment1" 
       android:id="@+id/fragment1" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_above="@id/statusUpdated" 
     /> 

     <fragment android:name="com.fragment2" 
       android:id="@+id/fragment2" 
       android:layout_width="0px" 
       android:layout_weight="2" 
       android:layout_height="fill_parent" 
       android:layout_above="@id/statusUpdated"   
       android:layout_toRightOf="@id/fragment1" 
     /> 

    </LinearLayout> 

    <TextView android:id="@+id/statusUpdated" style="@style/Status" /> 

</RelativeLayout> 

Trả lời

5

Bạn không thể sử dụng trọng với một RelativeLayout. Về cơ bản, thuộc tính đó bị bỏ qua, có nghĩa là cả hai đoạn của bạn hiển thị với chiều rộng là 0, do đó chúng không hiển thị.

Tôi không chắc chắn những gì bạn đang cố gắng hoàn thành, nhưng bạn có thể muốn xem xét gói ví dụ đầu tiên của mình (LinearLayout) thành một RelativeLayout - nói cách khác: kết hợp/tích hợp cả bố cục. Điều đó dẫn đến một cấp độ khác trong hệ thống phân cấp khung nhìn của bạn.


Edit:

tôi đã không thực sự thử nghiệm nó, nhưng tôi nghĩ một cái gì đó như thế này là những gì bạn đang tìm kiếm:

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

    <LinearLayout 
     android:id="@+id/fragment_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@+id/footer_view" 
     android:weightSum="3"> 

     <fragment 
      android:id="@+id/fragmentTitles" 
      android:name="com.fragment.test.TitlesFragment" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 

     <FrameLayout 
      android:id="@+id/details" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" /> 
    </LinearLayout> 

    <TextView 
     android:id="@+id/footer_view" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:background="#f00" 
     android:text="I am a footer" /> 

</RelativeLayout> 

Rõ ràng, bạn có thể thay thế TextView với bất cứ điều gì bạn thích.

+0

Điều cuối cùng tôi đang cố gắng làm là thêm chân trang vào cuối bố cục, nhưng tôi không thể làm cho nó hoạt động bằng cách sử dụng 'LinearLayout'. Tôi đã có thể lấy chân để hiển thị bằng cách sử dụng 'RelativeLayout' nhưng sau đó các mảnh vỡ sẽ không xuất hiện, vì vậy tôi chỉ cố gắng để có được những mảnh vỡ xuất hiện (không có chân), do đó câu hỏi của tôi. Có vẻ như tôi sẽ phải tìm ra cách để chân trang làm việc bằng cách sử dụng 'LinearLayout'. Về cơ bản, tôi muốn bố cục nơi phần bên trái chiếm 1/3 màn hình, phần còn lại của màn hình, với chân trang dính. –

+0

Phải, điều đó không quá khó để nhận ra. Xin vui lòng xem các chỉnh sửa trong câu trả lời của tôi và cho rằng một đi. Đã không thử nó, nhưng nó phải là (gần) những gì bạn đang sau. –

+0

Điều đó hiệu quả. Cảm ơn! –

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