Relativelayout 中的滚动视图在 android 中无法正确滚动

Scrollview inside Relativelayout is not scrolling properly in android

在我的屏幕设计中,我有一个操作栏和底部栏(基本上是一个线性布局)。在屏幕中间,我有几个文本视图和 4-5 个按钮。我想在屏幕的中间部分添加滚动条。但它不起作用。

我的代码在这里:

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

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/toAddrdrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/fromAddrDrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/toAddrdrop"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/distanceToCoverDrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/fromAddrDrop"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/distanceToCoverDrop"
                android:orientation="vertical">

                <Button
                    android:id="@+id/drop_confirm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Drop Confirm" />

                <Button
                    android:id="@+id/sai_out_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Sai Out" />

                <Button
                    android:id="@+id/customer_in_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer In" />

                <Button
                    android:id="@+id/customer_out_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer Out" />

                <Button
                    android:id="@+id/customer_new_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer new" />

                <Button
                    android:id="@+id/customer_new_drop22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer new 22" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/buttombardrop"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_alignParentBottom="true"
        android:background="#33B5E5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/callButton_drop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@android:drawable/ic_menu_call"
            android:paddingTop="8dp"
            android:text="Call"
            android:textColor="#FFFFFF" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#ffffff" />

        <Button
            android:id="@+id/mapButton_drop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@android:drawable/ic_menu_mapmode"
            android:paddingTop="8dp"
            android:text="Map"
            android:textColor="#FFFFFF" />

    </LinearLayout >

</RelativeLayout>

我观察到,我可以滚动到 4 个按钮。我该如何修复滚动视图。 请帮忙!!

您应该注意 RelativeLayout 多次出现意外行为。所以你应该使用 LinearLayout.

只需将您的代码替换为

<?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:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

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

            <TextView
                android:id="@+id/toAddrdrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/fromAddrDrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/toAddrdrop"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/distanceToCoverDrop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/fromAddrDrop"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="10dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/distanceToCoverDrop"
                android:orientation="vertical">

                <Button
                    android:id="@+id/drop_confirm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Drop Confirm" />

                <Button
                    android:id="@+id/sai_out_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Sai Out" />

                <Button
                    android:id="@+id/customer_in_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer In" />

                <Button
                    android:id="@+id/customer_out_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer Out" />

                <Button
                    android:id="@+id/customer_new_drop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer new" />

                <Button
                    android:id="@+id/customer_new_drop22"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="40dp"
                    android:background="@drawable/button_active"
                    android:text="Customer new 22" />
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/buttombardrop"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:background="#33B5E5"
        android:orientation="horizontal">

        <Button
            android:id="@+id/callButton_drop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@android:drawable/ic_menu_call"
            android:paddingTop="8dp"
            android:text="Call"
            android:textColor="#FFFFFF" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="#ffffff" />

        <Button
            android:id="@+id/mapButton_drop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:drawableTop="@android:drawable/ic_menu_mapmode"
            android:paddingTop="8dp"
            android:text="Map"
            android:textColor="#FFFFFF" />

    </LinearLayout >

</LinearLayout>