2011-09-04 42 views
9

Tôi đang cố gắng tạo bố cục Android: 3 thành phần bên trong LinearLayout dọc. Thành phần trung tâm là ScrollView có chứa TextView. Khi số TextView chứa một lượng văn bản đáng kể (nhiều hơn có thể vừa trên màn hình), thì ScrollView sẽ tăng lên tất cả các cách ở cuối màn hình, hiển thị thanh cuộn và đẩy thành phần cuối cùng, LinearLayout với một bên là Button màn.

Nếu văn bản bên trong TextView bên trong ScrollView đủ ngắn, nút ở cuối màn hình được đặt ở vị trí hoàn hảo.

Cách bố trí Tôi đang cố gắng để đạt được là:Khó khăn với ScrollView và LinearLayout

XML cho việc bố trí tôi đã viết là:

<?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"> 

    <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_marginBottom="10dip" 
      android:text="Title /> 

    <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

     <TextView android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:autoLink="web" 
       android:textColor="#FFFFFF" 
       android:background="#444444" 
       android:padding="10dip" /> 

    </ScrollView> 

    <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1"> 

     <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_weight="1"/> 

     <Button android:id="@+id/login_button" 
       android:layout_width="0dip" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:layout_weight="1" 
       android:text="@string/next_button"/> 

    </LinearLayout> 

</LinearLayout> 

Trả lời

6

Các scrollview là đối tượng xem thứ hai và được đặt thành wrap_content, nhiều hơn màn hình.

Tôi khuyên bạn nên sử dụng RelativeLayout. Đầu xem văn bản đầu tiên với android:alignParentTop="true", dưới cùng LinearLayout tiếp theo với android:alignParentBottom="true" và scrollview được liệt kê cuối cùng trong xml với giá trị android:alignBelow="@id/whatYouCallTheHeader.

Điều này sẽ căn chỉnh thanh dưới cùng ở cuối màn hình và tiêu đề ở trên cùng, bất kể kích thước. Sau đó, scrollview sẽ có vị trí riêng của nó, sau khi tiêu đề và chân trang đã được đặt.

+0

Nếu bạn đang viết cho 1.6 như một mục tiêu nó chỉ làm cho người ta đi qua xml, vì vậy bất kỳ tài liệu tham khảo phải được đặt ra trước khi nó được tham chiếu. 2.1+ tạo hai đường chuyền. – Phobos

2

bạn nên truy cập relativeLayout thay vì LinearLayout. Và bạn có thể sử dụng một số thuộc tính như alignBelow và tất cả.

2

Thử thêm trọng lượng bố cục vào ví dụ ScrollView.

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 

này làm việc cho tôi trong một tình huống gần như giống hệt với một bạn đang trình bày nhưng để lại cho tôi tự hỏi tại sao, bởi vì nó là phản trực giác rằng việc tăng trọng lượng bố trí của một điều khiển từ 0 (mặc định nếu bạn không chỉ định một số layout_weight) cho 1 nên tạo điều khiển đã sử dụng quá nhiều không gian nhỏ hơn.

Tôi nghi ngờ lý do nó hoạt động là do không chỉ định layout_weight, bạn thực sự cho phép bố cục bỏ qua kích thước của chế độ xem cuộn tương đối so với các điều khiển khác và ngược lại nếu bạn chỉ định cho phép thu nhỏ nó theo tỷ lệ trọng lượng bạn chỉ định.

2

! [Cố định header và footer và bố trí cơ thể cuộn] [1]


Đây là những gì bạn đang tìm kiếm. Hầu hết ứng dụng trong android đều có kiểu bố cục này, đầu trang và chân trang cố định và một phần có thể cuộn được. Xml cho bố trí này là


<?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:background="#5599DD" 
     android:layout_height="fill_parent"> 
     <!-- Header goes here --> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:layout_marginLeft="10dip" 
      android:layout_marginRight="10dip" 
      android:layout_marginTop="10dip" 
      android:layout_marginBottom="10dip" 
      android:textSize="20sp" 
      android:layout_gravity="center" 
      android:text="Title" /> 
     <!-- Body goes here --> 
     <ScrollView 
      android:layout_weight="1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:autoLink="web" 
       android:text="@string/lorem_ipsum" 
       android:textColor="#FFFFFF" 

       android:padding="10dip" /> 
     </ScrollView> 
     <!-- footer goes here --> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <RelativeLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
       <Button 
        android:id="@+id/login_button" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_gravity="bottom" 
        android:layout_alignParentRight="true" 
        android:text="Button"/> 

      </RelativeLayout> 
    </LinearLayout> 
</LinearLayout> 
Các vấn đề liên quan