How do you make a LinearLayout scrollable?

前端 未结 10 1778
清歌不尽
清歌不尽 2020-11-28 18:55

I have lot of items on the screen and I need to use the scrollbar so the user can scroll down. However, the scroll is either not visible or it\'s not working. How is it poss

相关标签:
10条回答
  • 2020-11-28 19:02

    Wrap the linear layout with a <ScrollView>

    See here for an example:

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout 
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           xmlns:android="http://schemas.android.com/apk/res/android">
           <ScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
                <LinearLayout 
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:orientation="vertical">
                      <!-- Content here -->
                </LinearLayout>
          </ScrollView>
     </LinearLayout>
    

    Note: fill_parent is deprecated and renamed to match_parent in API Level 8 and higher.

    0 讨论(0)
  • 2020-11-28 19:03

    you need to use the following attribute and enclose it within the linear layout

    <LinearLayout ...>
    <scrollView ...> 
    
    </scrollView>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-28 19:04
    <ScrollView 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/scroll" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
          <LinearLayout 
                android:id="@+id/container"
                android:orientation="vertical" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
          </LinearLayout>
    
     </ScrollView>
    
    0 讨论(0)
  • 2020-11-28 19:07

    Whenever you wanted to make a layout scrollable, you can use <ScrollView> With a layout or component in it.

    0 讨论(0)
  • 2020-11-28 19:09

    This can be done using the tag <ScrollView>. For ScrollView, one thing you have to remind that, ScrollView must have a single child.

    If you want your full layout to be scrollable then add <ScrollView> at the top. Check the example given below.

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scroll" 
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout 
            android:id="@+id/container" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
                <!-- Content here -->
        </LinearLayout>
    
    </ScrollView>
    

    But if you want some part of your layout to be scrollable then add <ScrollView> within that part. Check the example given below.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="400dp">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                    <!-- Content here -->
            </LinearLayout>
    
        </ScrollView>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-28 19:09

    You need to place ScrollView as the first child of Layout file and now put your linearlayout inside it. Now, android will decide on the basis of content and device size available whether to show a scrollable or not.

    Make sure linearlayout has no sibling because ScrollView can not have more than one child.

    0 讨论(0)
提交回复
热议问题