Làm cách nào để tôi có thể cuộn tuyến tínhLayout?


155

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ó ai biết làm thế nào để cuộn này?

<?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);
    }
}

Câu trả lời:


229

Đặt bố cục của bạn trong ScrollView .


7
Đây là tác dụng phụ, chẳng hạn như không thể đặt các yếu tố có thể cuộn bên trong cuộn xem, chẳng hạn như listview.
JoaoFilipeClementeMartins

4
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

@JoaoFilipeClementeMartins nếu bạn sẽ không được hưởng lợi từ chức năng bộ điều hợp của ListView, thì bạn chỉ cần thêm tất cả các thành phần trong danh sách của mình vào trình bao bọc tuyến tính để tạo cảm giác tất cả được sắp xếp hợp lý trong hộp có thể cuộn cùng với các yếu tố khác.
Atharva

Điều đó sẽ không tạo ra một bố cục có thể cuộn tốt, đặc biệt là nếu bạn có các tab và muốn di chuyển giữa chúng. Nó sẽ không di chuyển đúng cách và nó cũng sẽ hiển thị một thanh ở bên cạnh trong khi bạn cuộn
Luôn luôn được sử dụng vào

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

    <LinearLayout ...>

        ...
        ...

    </LinearLayout>

</ScrollView>

65
<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>

17

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

ScrollView chỉ có một bố cục như con của nó.

isScrollContainer="true"

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


3

Bạn cũng có thể triển khai nó bằng View.scrollTo (..) .

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

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

                    llParent.scrollTo(counter , 0);
                }

            }
        }, 1000L);

2

bạn có thể làm cho bất kỳ bố trí có thể cuộn. Chỉ cần <?xml version="1.0" encoding="utf-8"?>thêm các dòng sau:

<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ề hoạt động không thể 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 làm cho nó có thể cuộn, nó trở thành 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ả các bố cục của bạn bên trong ScrollView với chiều rộng và chiều cao được đặt thành fill_parent.


Câu trả lời của bạn trông giống như một bình luận hơn là một câu trả lời. Một khi bạn có đủ danh tiếng, bạn sẽ có thể nhận xét về bất kỳ bài đăng nào. Ngoài ra kiểm tra điều này những gì tôi có thể làm thay thế . Nếu bạn có ý định đưa ra câu trả lời, hãy đọc cách trả lời này để cung cấp câu trả lời chất lượng.
thewaywewere

fill_parent đã không dùng nữa và được thay thế bằng match_parent
Vòng lặp vô hạn

0

thử mã dưới đây

 <?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/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.blah"
    >

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

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:background="#000044"
            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></ScrollView></RelativeLayout>
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.