【问题标题】:Is it possible to make a linear layout scrollable?是否可以使线性布局可滚动?
【发布时间】:2013-02-25 03:03:59
【问题描述】:

我创建了一个垂直线性布局并在其中放置了许多项目,并且其中只有一部分可以显示在屏幕上。我可以看到布局的前几项,但看不到布局的最后几项。 如何使线性布局可滚动,以便用户可以滚动屏幕以查看线性布局的最后几个项目? 以下是布局xml文件的内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView_gridw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Grid Width"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_gridw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/textView_gridh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Height"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_gridh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView_gridbt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Border thickness"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_gridborderthickness"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView_bgw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="BgImg Width"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_bgw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:focusable="false"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView_bgh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Height"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_bgh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:focusable="false"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView_mtgw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="MTG Width"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_mtgw"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView_mtgh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="Height"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/editText_mtgh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Margin(left, top, right, bottom):"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/editText_margin_l"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        <EditText
            android:id="@+id/editText_margin_t"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        <EditText
            android:id="@+id/editText_margin_r"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

        <EditText
            android:id="@+id/editText_margin_b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView_pathname_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Background image file path name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText_pathname_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <TextView
        android:id="@+id/textView_pathname_thumbdir"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Thumbnail images directory path name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText_pathname_thumbdir"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <TextView
        android:id="@+id/textView_pathname_treasure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Treasure image file path name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText_pathname_treasure"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <TextView
        android:id="@+id/textView_pathname_signature"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Signature image file path name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText_pathname_signature"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="reset"
            android:text="Reset" />
        <Button
            android:id="@+id/button_apply"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="apply"
            android:text="Apply" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

标签: android android-layout


【解决方案1】:

您应该将您的LinearLayout 包装在ScrollView 中:

用户可以滚动的视图层次结构的布局容器,允许它大于物理显示。 ScrollView 是 FrameLayout,这意味着您应该在其中放置一个包含要滚动的全部内容的子视图;这个孩子本身可能是一个具有复杂对象层次结构的布局管理器。一个经常使用的子元素是垂直方向的 LinearLayout,呈现用户可以滚动浏览的顶级项目的垂直数组。

【讨论】:

    【解决方案2】:

    你可以使用ScrollView,如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <TextView
                    android:id="@+id/textView_gridw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="Grid Width"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_gridw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" >
    
                    <requestFocus />
                </EditText>
    
                <TextView
                    android:id="@+id/textView_gridh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="Height"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_gridh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
    
                <TextView
                    android:id="@+id/textView_gridbt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="Border thickness"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_gridborderthickness"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <TextView
                    android:id="@+id/textView_bgw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="BgImg Width"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_bgw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:focusable="false"
                    android:inputType="number" />
    
                <TextView
                    android:id="@+id/textView_bgh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="Height"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_bgh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:focusable="false"
                    android:inputType="number" />
    
                <TextView
                    android:id="@+id/textView_mtgw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="MTG Width"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_mtgw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
    
                <TextView
                    android:id="@+id/textView_mtgh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="0"
                    android:text="Height"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
    
                <EditText
                    android:id="@+id/editText_mtgh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <TextView
                    android:id="@+id/textView_margin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Margin(left, top, right, bottom):"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <EditText
                    android:id="@+id/editText_margin_l"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
    
                <EditText
                    android:id="@+id/editText_margin_t"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
    
                <EditText
                    android:id="@+id/editText_margin_r"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
    
                <EditText
                    android:id="@+id/editText_margin_b"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="number" />
            </LinearLayout>
    
            <TextView
                android:id="@+id/textView_pathname_bg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Background image file path name:"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <EditText
                android:id="@+id/editText_pathname_bg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />
    
            <TextView
                android:id="@+id/textView_pathname_thumbdir"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Thumbnail images directory path name:"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <EditText
                android:id="@+id/editText_pathname_thumbdir"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />
    
            <TextView
                android:id="@+id/textView_pathname_treasure"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Treasure image file path name:"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <EditText
                android:id="@+id/editText_pathname_treasure"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />
    
            <TextView
                android:id="@+id/textView_pathname_signature"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Signature image file path name:"
                android:textAppearance="?android:attr/textAppearanceMedium" />
    
            <EditText
                android:id="@+id/editText_pathname_signature"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
    
                <Button
                    android:id="@+id/button_reset"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="reset"
                    android:text="Reset" />
    
                <Button
                    android:id="@+id/button_apply"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="apply"
                    android:text="Apply" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
    

    【讨论】:

    • ScrollView 工作正常,我的问题已经解决。谢谢大家。
    • 致 Jules Mazur,我试过了,但未能删除这个问题。它说“对不起,这个问题有答案,不能删除......”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多