Android,让按钮忽略父级填充?

时间:2015-12-08 20:53:54

标签: android xml

我有一个滚动视图,里面是线性布局,在线性布局中有一些按钮。我为线性布局定义了一些填充,但滚动时,按钮会忽略它并与滚动视图重叠。该怎么办? 这是xml:

<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="0dp"
    android:layout_height="140dp"
    android:layout_weight="1"
    android:background="@drawable/item"
    android:fadeScrollbars="false"
    android:fadingEdge="none">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/item"
        android:gravity="center"
        android:orientation="vertical">

        <Button
            android:id="@+id/Button03"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ff0000"
            android:text="Button" />

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

        <Button
            android:id="@+id/Button02"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ff0000"
            android:text="Button" />

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

        <Button
            android:id="@+id/Button01"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ff0000"
            android:text="Button" />

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

        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:background="#ff0000"
            android:text="Button" />
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

这是因为填充空间是滚动视图的一部分。线性布局在滚动视图内部,填充空间在线性布局内,滚动视图内部的任何内容都将滚动。因此,填充空间也会滚动。

如果要保留填充空间,请添加填充空间以滚动视图。

代码:

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:fadeScrollbars="false"
        android:fadingEdge="none" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:background="@mipmap/ic_launcher"
            android:orientation="vertical">

            <Button
                android:id="@+id/Button03"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                android:background="#ff0000"
                android:text="Button" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#1e90ff"/>

            <Button
                android:id="@+id/Button02"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                android:background="#ff0000"
                android:text="Button" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#1e90ff"/>

            <Button
                android:id="@+id/Button01"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                android:background="#ff0000"
                android:text="Button" />
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#1e90ff"/>

            <Button
                android:id="@+id/button1"
                android:layout_width="fill_parent"
                android:layout_height="250dp"
                android:background="#ff0000"
                android:text="Button" />

        </LinearLayout>
    </ScrollView>

现在,填充空间不会被按钮覆盖。 希望它能解决你的问题。