2011-01-13 6 views
6

Tôi muốn tạo bố cục, với một LinearLayout nằm ngang ở trên và dưới, một ListView điền vào giữa.Bố cục vấn đề: cách đặt thứ gì đó ở trên và dưới?

Làm cách nào để xác định tệp main.xml.

Tôi đã cố gắng tạo bố cục với LinearLayout nằm ngang ở trên cùng, TextView ở phía dưới, một ListView điền ở giữa; được rồi. Nhưng sau khi tôi sửa đổi TextView dưới cùng thành LinearLayout, LinearLayout dưới cùng biến mất.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     > 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="50px" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="bottom" 
     > 
     <ListView 
      android:id="@+id/listbody" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      /> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      > 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
       /> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
       /> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

Mọi người đều có thể cho biết lời khuyên? Xin vui lòng giúp đỡ.

Trả lời

11

Thử chứa toàn bộ trong một RelativeLayout:

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

    <LinearLayout 
     android:id="@+id/top_linear_layout_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" > 
    </LinearLayout> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/bottom_linear_layout_id" 
     android:layout_below="@id/top_linear_layout_id" /> 

    <LinearLayout 
     android:id="@+id/bottom_linear_layout_id" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" > 
    </LinearLayout> 
</RelativeLayout> 

Chỉnh sửa với công cụ kích thước.

+0

Bây giờ LinearLayout chồng lên với ListView! Tôi nghĩ rằng nó phải là một cái gì đó sai trái trong việc defination của LinearLayout ở phía dưới. – chow

+0

Không phải đặc biệt. Đối với ListView, thêm: \t android: layout_height = "wrap_content" \t android: layout_width = "fill_parent" \t android: layout_below = "@ id/top_linear_layout_id" \t android: layout_above = "@ + id/bottom_linear_layout_id" Cùng với các id tương ứng cho các bố trí tuyến tính. – Estel

0

Vấn đề là bạn định nghĩa cả hai nội dung có liên quan đến kích thước nội dung chứa, nhưng nếu nội dung kết hợp của cả hai lớn hơn màn hình thì một trong số chúng sẽ chồng lên nhau, sẽ tốt hơn để xác định layout_height bằng cách sử dụng phép đo tuyệt đối trong bố cục tuyến tính

3

Dưới đây là những gì bạn đang tìm kiếm. Estel đang đi đúng hướng với RelativeLayout (mặc dù nó có thể được thực hiện với LinearLayout, tuy nhiên tôi nghĩ phương pháp RelativeLayout là sạch hơn) nhưng bố cục không theo thứ tự Nevermind, hãy kiểm tra bình luận của Estel. :) Trước tiên, bạn nên xác định tiêu đề của mình, căn chỉnh đầu trang đó; tiếp theo, xác định chân trang của bạn và căn chỉnh nó ở cuối; cuối cùng, hãy khai báo ListView của bạn, đặt nó ở độ cao của fill_parent và đặt nó vào bố cục phía trên chân trang và bên dưới tiêu đề:

<?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/header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:alignParentTop="true" 
     > 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/footer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:alignParentBottom="true" 
     > 
    </LinearLayout> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_above="@id/footer" 
     android:layout_below="@id/header" 
     /> 
</RelativeLayout> 
+0

Không quan tâm, mức độ quan trọng của đơn đặt hàng trong trường hợp này là bao nhiêu? Trong khi chúng tôi đang rõ ràng đảm bảo rằng không có sự chồng chéo trong vị trí của bất kỳ vật dụng nào, không phải thứ tự bản vẽ ngày càng trở nên dư thừa? – Estel

+0

Đọc tại đây: http://developer.android.com/guide/topics/ui/index.html#ViewHierarchy nó ​​nói rằng bố cục được phân tích cú pháp theo thứ tự. Vì vậy, nếu ListView được khai báo trước bố cục chân trang, nó sẽ lấy tất cả không gian từ bên dưới tiêu đề, đến cuối màn hình. Sau đó, chân trang sẽ được vẽ trên đầu của ListView, vì ListView không nhận thức được chân trang tại thời điểm nó được phân tích cú pháp. Tôi muốn nói rằng tôi đã đọc nó như của Android 2.2, thứ tự không còn quan trọng (nhưng vẫn còn nếu bạn đang nhắm mục tiêu các phiên bản trước) nhưng tôi không thể tìm thấy một tham chiếu dứt khoát bây giờ. – kcoppock

+0

Một điều tôi đã tìm thấy http://developer.android.com/resources/articles/ui-1.6.html có thể thích hợp là 1,6 tài liệu tham khảo được kích hoạt về phía trước cho các tài nguyên được khai báo xa hơn xuống cây. Đây có phải là những gì bạn đang đề cập đến? – Estel

0

Cảm ơn lời khuyên và hướng dẫn của bạn về RelativeLayout. Tôi có thể sửa nó ngay bây giờ.

tôi phải xác định LinearLayout chân trước ListView, và xác định ListView như android: layout_alignParentTop = "true" và android: layout_above = "@ id/footer"

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
    > 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="50px" 
      android:layout_height="wrap_content" 
      /> 
     <TextView 
      android:textSize="12px" 
      android:text="something here" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
     /> 
    </LinearLayout> 
    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
     <LinearLayout 
      android:id="@+id/footer" 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" 
      android:layout_alignParentBottom="true" 
     > 

      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
       /> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12px" 
       android:text="50%" 
      /> 

     </LinearLayout> 
     <ListView 
      android:id="@+id/tablebody" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_above="@id/footer" 
     /> 
    </RelativeLayout> 
</LinearLayout> 
0

này chắc chắn sẽ giải quyết vấn đề của bạn :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
      <TextView 
       android:textSize="12sp" 
       android:text="something here" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content"/> 
      <TextView 
       android:textSize="12sp" 
       android:text="something here" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"/> 
     </LinearLayout> 
    </HorizontalScrollView> 
    <ListView 
     android:id="@+id/listbody" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 
    <HorizontalScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent"> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12sp" 
       android:text="50%"/> 
      <TextView 
       android:layout_height="wrap_content" 
       android:layout_width="0dip" 
       android:layout_weight="1" 
       android:textSize="12sp" 
       android:text="50%"/> 
     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout>