2011-10-10 40 views
9

này là ví dụ XMLLàm thế nào để ngăn chặn khác Xem bị đẩy ra khỏi màn hình

<?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 android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:orientation="vertical" 
     android:layout_margin="5dip" android:background="#FF992222"> 
     <ScrollView android:layout_height="wrap_content" 
      android:layout_width="fill_parent"> 
      <LinearLayout android:layout_height="wrap_content" 
       android:orientation="vertical" android:layout_width="fill_parent"> 
       <TextView android:layout_width="wrap_content" android:text="TextView" 
        android:layout_height="wrap_content" android:textSize="24dip"></TextView> 
      </LinearLayout> 
     </ScrollView> 
    </LinearLayout> 
    <LinearLayout android:layout_width="match_parent" 
     android:layout_height="wrap_content" android:orientation="vertical" 
     android:layout_margin="5dip" android:background="#FF555555"> 
     <TextView android:layout_width="wrap_content" android:text="TextView" 
      android:layout_height="wrap_content" android:textSize="24dip"></TextView> 
    </LinearLayout> 
</LinearLayout> 

Kết quả là như thế này:

enter image description here

Nếu bạn nhận thấy, các văn bản trong LinerLayout trên được gói trong LinearLayout và ScrollView.

Nếu nội dung trên tiếp tục gia tăng, nó sẽ giống như thế này

enter image description here

dưới LinearLayout sẽ được đẩy ra khỏi màn hình trước khi scrollview trở nên năng động và làm cho nội dung đầu tiên trở thành cuộn .... và tôi không muốn điều đó xảy ra được ...

tôi nên làm gì để làm cho scrollview trước Xem đáy bị đẩy ra khỏi màn hình như thế này:

(điều này được chỉnh sửa hình ảnh và không được tạo ra bởi trình biên tập Layout Android)

enter image description here

Hy vọng câu hỏi đủ rõ ràng .. Cảm ơn :)

+0

tại sao không bạn giữ mọi thứ bên trong một scrollview. –

+0

vì vậy bạn không muốn di chuyển chế độ xem văn bản màu xám ở tất cả trong khi những màu đỏ ở lại có thể cuộn am i chính xác? – Urban

Trả lời

3

Vâng, nếu tôi hiểu rõ câu hỏi của bạn, bạn muốn TextView có màu xám vẫn giữ nguyên, trong khi TextView có màu đỏ có thể cuộn quanh, sửa tôi nếu tôi sai ở đây. Nếu đó là những gì bạn muốn, sau đó bạn có thể sử dụng RelativeLayout để đạt được điều đó. Sau đây là mã để tạo ra một đầu ra như:
http://imageshack.us/photo/my-images/836/device20111011012652.png/
Các mục ListView có thể cuộn được trong khi nút (trong trường hợp của bạn là TextView) vẫn ở đâu.

Code:

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

<ListView android:id="@android:id/list" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_above="@+id/newtask" android:layout_alignParentTop="true"> 
</ListView> 

<Button android:layout_width="fill_parent" 
android:id="@+id/newtask" 
android:layout_height="wrap_content" 
android:text="Add New Task" android:layout_alignParentBottom="true"> 
</Button> 
</RelativeLayout> 

Chỉ cần thay đổi vào nút dưới lên một TextView và nó sẽ phù hợp với những gì bạn muốn.

+0

Wow tuyệt vời .... Đó không phải là câu trả lời tôi muốn ... Nhưng nó thực sự cho tôi biết làm thế nào RelativeLayout có thể làm ...:) Trong xml của bạn, khi 'android: layout_alignParentBottom =" true "' được gọi, hàm relativeLayout trở thành fill_parent và tôi không muốn. Thay vào đó tôi loại bỏ nó và thêm 'android: layout_below =" @ + id/box1' vào hộp dưới cùng để làm cho nó như tôi muốn ... :) –

+0

oh no ... nó không giải quyết được vấn đề của tôi .... Điều này tôi muốn đạt được ... Tôi muốn hộp đầu tiên trở nên linh hoạt ... vì vậy khi hộp đầu tiên nội dung chỉ 1/2 dòng, nó sẽ trông giống như pic 1. và khi nội dung nhận được quá nhiều, nó sẽ giống như 3rd pic .... –

+1

ok điều này có thể là một chút mặc dù để thực hiện, nhưng nếu bạn đang sử dụng ListViews (lý tưởng bạn nên sử dụng trong trường hợp của bạn) thì bạn có thể ghi đè onScroll listener và kiểm tra xem mục cuối cùng trong danh sách là Nếu có thể nhìn thấy, thì bạn sẽ phải lập trình căn chỉnh TextView màu xám ở dưới cùng của mục cuối cùng, nếu không thiết lập căn chỉnh ở dưới cùng của màn hình (như được hiển thị trong câu trả lời). of listveiw: http://stackoverflow.com/questions/5123675/find-out-if-listview-is-scrolled-to-the-bottom – Urban

4

Hãy thử điều này. Điều quan trọng là sử dụng layout_weight trong bố cục tuyến tính.

<?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"> 
    <ScrollView android:layout_height="0dp" 
     android:layout_weight="1" 
     android:layout_width="fill_parent"> 
     .... 
    </ScrollView> 
    <TextView android:layout_width="fill_parent" android:text="TextView" 
     android:layout_height="wrap_content" android:textSize="24dip"/> 
</LinearLayout> 
+0

Tôi biết tôi có thể đạt được lần chia sẻ màn hình cuối cùng với layout_weight. Nhưng tôi muốn hộp đầu tiên trở nên linh hoạt .. nó sẽ giữ nhỏ nếu nội dung chỉ là 1/2 dòng, và có thể cuộn khi nội dung nhận được quá nhiều ... –

+0

Hộp đầu tiên sẽ linh hoạt nếu bạn thay đổi 'android: layout_height = "fill_parent" 'thành' android: layout_height = "wrap_content" 'trong LinearLayout đầu tiên. Tôi nghĩ. –

+0

Làm việc cho tôi .. Cảm ơn –

7

Tôi mất một khoảng thời gian điên rồ để tìm ra điều này, không có thông tin trên mạng, nhưng cuối cùng tôi đã nhận được nó. Thưởng thức!

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mainlayout" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_gravity="center_vertical" 
android:orientation="vertical" 
> 

<LinearLayout 
    android:id="@+id/LL" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical"     
    android:gravity="center_vertical" 
    android:layout_centerVertical="true" 
    android:orientation="vertical" 
    android:weightSum="1" > 


    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/TV1" 
     android:layout_margin="5dp" 
     android:layout_weight="1" 
     android:fillViewport="true" > 

     <TextView 
      android:id="@+id/TV2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" /> 
    </ScrollView> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="5dp" 
     android:layout_weight="0" 
     android:orientation="horizontal" > 

     <TextView android:layout_width="wrap_content" android:text="TextView" 
     android:layout_height="wrap_content" android:textSize="24dip"></TextView> 

    </LinearLayout> 
</LinearLayout> 

+1

Đây phải là câu trả lời được chấp nhận vì nó hoạt động. –

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