可滚动布局不起作用

scrollable layout not working

我是 android 的初学者。我正在使用 Eclipse 创建一个示例电话簿。当我 运行 项目时,我的 layout.but 中几乎没有编辑文本和一个关闭按钮 我看不到布局底部的按钮,所以我将我的主要线性布局包含在滚动视图中.但是在线性布局的高度变得更大之后。我试图在 xml 文件中调整它的大小,但在调整布局大小后它无法正常工作,它会自动调整到以前的大小。如果有人可以提供帮助,这是我的代码 me.little 非常感谢帮助。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="466dp"
android:layout_gravity="center_vertical" >

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    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:scrollbarAlwaysDrawVerticalTrack="false"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="vertical"
    tools:context="com.nadusha.phonebook.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginBottom="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact Info"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

</LinearLayout>



<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />

</LinearLayout>

<LinearLayout
    android:id="@+id/nameTextLayout"
    android:orientation="horizontal"
    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <EditText
        android:id="@+id/nameTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />
    <requestFocus />

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mobile"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="#FFFFFF"/>

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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


    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />
</LinearLayout>

<LinearLayout

    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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

</LinearLayout>



<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-Mail "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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

</LinearLayout>

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

    <Button
        android:id="@+id/saveBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" 
        android:textColor="#FFFFFF"/>

    <Button
        android:id="@+id/cancelBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:textColor="#FFFFFF" />


       <Button
        android:id="@+id/viewBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ViewContact"
        android:textColor="#FFFFFF" />

</Linearlayout> 
</LinearLayout>

您需要将 ScrollView layoutWeight 设置为 1,并将其高度设置为 0。这将确保您的 scrollview 在屏幕上只占用剩余的 space。这是我的应用示例:

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

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:clickable="false"
        android:fadeScrollbars="false"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="5dp">

                <TextView
                    android:id="@+id/menoLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/emergencySMSMeno"/>

                <EditText
                    android:id="@+id/menoEdit"
                    style="@style/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="textPersonName"
                    android:textColor="#000"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="5dp"
                android:weightSum="3">

                <TextView
                    android:id="@+id/pohlavieLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/emergencySMSPohlavie"/>

                <Spinner
                    android:id="@+id/spinnerPohlavie"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="5dp">

                <TextView
                    android:id="@+id/vekLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/emergencySMSVek"/>

                <EditText
                    android:id="@+id/vekEdit"
                    style="@style/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="numberDecimal"
                    android:textColor="#000"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="5dp">

                <TextView
                    android:id="@+id/vahaLabel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/emergencySMSVaha"/>

                <EditText
                    android:id="@+id/vahaEdit"
                    style="@style/editText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:inputType="numberDecimal"
                    android:textColor="#000"/>
            </LinearLayout>

            <TextView
                android:id="@+id/alergieLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:padding="5dp"
                android:text="@string/emergencySMSAlergie"/>

            <EditText
                android:id="@+id/alergieEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:textColor="#000"/>

            <TextView
                android:id="@+id/chorobyLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:padding="5dp"
                android:text="@string/emergencySMSChoroby"/>

            <EditText
                android:id="@+id/chorobyEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:textColor="#000"/>

            <TextView
                android:id="@+id/zraneniaLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:padding="5dp"
                android:text="@string/emergencySMSZranenie"/>

            <EditText
                android:id="@+id/zraneniaEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textMultiLine"
                android:lines="3"
                android:textColor="#000"/>

            <TextView
                android:id="@+id/polohaLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:padding="5dp"
                android:text="@string/emergencySMSAktualnaPoloha"/>

            <EditText
                android:id="@+id/polohaEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:textColor="#000"/>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dp">

        <Button
            android:id="@+id/odoslatButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/send"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/zrusitButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/emergencySMSZrusit"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

它显示了 scrollView 和它下面的两个按钮。我不确定你想做什么,但据我所知你做错了。 ScrollView 只能采用一个 child 元素,但您已将 scrollview 设置为主要 parent 但随后您在其中放置了更多 child 。

你又犯了一个错误。如果线性布局只有一个 child,则无需使用线性布局(在大多数情况下)。这对性能不利。

您可以尝试更改

android:layout_height="466dp"

android:layout_height="wrap_content"

android:layout_height="match_parent"

将属性 layout_height 的值更改为 match_parent 以便 ScrollView 占据整个屏幕,从而启用滚动。正确的代码是

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

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    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:scrollbarAlwaysDrawVerticalTrack="false"
    android:scrollbarStyle="insideOverlay"
    android:scrollbars="vertical"
    tools:context="com.nadusha.phonebook.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_marginBottom="10dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact Info"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF" />

</LinearLayout>



<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />

</LinearLayout>

<LinearLayout
    android:id="@+id/nameTextLayout"
    android:orientation="horizontal"
    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <EditText
        android:id="@+id/nameTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />
    <requestFocus />

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mobile"
        android:textAppearance="?android:attr/textAppearanceSmall" 
        android:textColor="#FFFFFF"/>

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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


    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />
</LinearLayout>

<LinearLayout

    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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

</LinearLayout>



<LinearLayout
    android:layout_marginBottom="8dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="E-Mail "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="#FFFFFF" />

</LinearLayout>

<LinearLayout
    android:layout_marginBottom="15dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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

</LinearLayout>

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

    <Button
        android:id="@+id/saveBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" 
        android:textColor="#FFFFFF"/>

    <Button
        android:id="@+id/cancelBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:textColor="#FFFFFF" />


       <Button
        android:id="@+id/viewBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ViewContact"
        android:textColor="#FFFFFF" />

</Linearlayout> 
</LinearLayout>