2010-08-05 32 views
139

Sau khi tôi bắt đầu hoạt động, tôi không thể cuộn xuống để xem các nút và tùy chọn khác trong xml được xác định bên dưới.Cách tạo LinearLayout có thể cuộn

Có ai biết cách tạo cuộn này không?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:background="#000044" 
android:isScrollContainer="true" 
android:orientation="vertical"> 
<TextView 
    android:id="@+id/title" 
    android:text="@string/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<EditText 
    android:id="@+id/editTitle" 
    android:text="" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/description" 
    android:text="@string/description" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<EditText 
    android:id="@+id/editDescription" 
    android:text="" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/location" 
    android:text="@string/location" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<EditText 
    android:id="@+id/editLocation" 
    android:text="" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/startTime" 
    android:text="@string/startTime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<DatePicker 
    android:id="@+id/DatePicker01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<TimePicker 
    android:id="@+id/TimePicker01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
<TextView 
    android:id="@+id/endTime" 
    android:text="@string/endTime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#ffffff"/> 
<DatePicker 
    android:id="@+id/DatePicker02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<TimePicker 
    android:id="@+id/TimePicker02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
<Button 
    android:id="@+id/buttonCreate" 
    android:text="Create" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</LinearLayout> 

mã lớp của tôi là

package com.example.blah; 

import android.app.Activity; 
import android.os.Bundle; 

public class example extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

Đầu ra là http://img265.imageshack.us/img265/7601/linearlayout.jpg

+0

Bản sao có thể có của [Làm thế nào để bạn tạo một LinearLayout có thể cuộn?] (Http://stackoverflow.com/questions/4055537/how-do-you-make-a-linearlayout-scrollable) – Flow

Trả lời

204

Nơi bố trí của bạn trong một ScrollView.

+0

Câu trả lời hoàn hảo! – Sana

+6

Đây là các tác dụng phụ, chẳng hạn như không thể đặt các phần tử cuộn vào bên trong một chế độ xem cuộn như chế độ xem danh sách. – JoaoFilipeClementeMartins

+2

Bạn nên tránh nội dung có thể cuộn bên trong nội dung có thể cuộn. Người dùng sẽ không thích nó (bạn không bao giờ biết bạn sẽ di chuyển cuộn nào). – Guillaume

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

<LinearLayout ...> 

... 
... 

</LinearLayout> 

</ScrollView> 
15

Đặt toàn bộ nội dung của bạn trong bố cục tuyến tính được đặt bên trong ScrollView.

ScrollView chỉ mất một bố cục làm con của nó.

isScrollContainer="true" 

Thuộc tính này được sử dụng khi phím chức năng của bạn trong Android bật lên và bạn vẫn muốn chế độ xem của mình cuộn.

2

Bạn cũng có thể triển khai nó bằng cách sử dụng View.scrollTo(..).

postDelayed(new Runnable() { 
      public void run() { 

       counter = (int) (counter + 10); 
       handler.postDelayed(this, 100); 

        llParent.scrollTo(counter , 0); 
       } 

      } 
     }, 1000L); 
54
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center|left" 
     android:orientation="vertical"> 

     Your views go here... 

    </LinearLayout> 

</ScrollView> 
2

bạn có thể thực hiện bất kỳ bố trí cuộn. Chỉ dưới <?xml version="1.0" encoding="utf-8"?> thêm những dòng này:

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

và cuối cùng thêm </ScrollView>

ví dụ về một hoạt động phi cuộn:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:verticalScrollbarPosition="right" 
    tools:context="p32929.demo.MainActivity"> 


    <TextView 
     android:text="TextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="102dp" 
     android:id="@+id/textView" 
     android:textSize="30sp" /> 
</RelativeLayout> 

Sau khi thực hiện nó cuộn, nó trở nên như thế này:

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

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

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:verticalScrollbarPosition="right" 
     tools:context="p32929.demo.MainActivity"> 


     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="102dp" 
      android:text="TextView" 
      android:textSize="30sp" /> 
    </RelativeLayout> 
</ScrollView> 
1

Đặt tất cả bố cục của bạn bên trong một ScrollView với chiều rộng và h tám bộ để fill_parent.

+0

Câu trả lời của bạn trông giống như một nhận xét chứ không phải là câu trả lời. Khi bạn có đủ [danh tiếng] (http://stackoverflow.com/help/whats-reputation), bạn sẽ có thể [nhận xét] (http://stackoverflow.com/help/privileges/comment) trên bất kỳ bài đăng nào. Ngoài ra, hãy kiểm tra [thay vào đó tôi có thể làm gì] (https: //meta.stackexchange.com/questions/214173/why-do-i-need-50-danh-nhận-cái-gì-thể-i-do-thay thế). Nếu bạn định đưa ra câu trả lời, hãy đọc [cách trả lời] này (http://stackoverflow.com/help/how-to-answer) để cung cấp câu trả lời có chất lượng. – thewaywewere

+0

fill_parent đã không được dùng nữa và được thay thế bằng match_parent –

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