简体   繁体   中英

Android LinearLayout is adding extra space at bottom

When I go to put elements in my LinearLayout and format them to fill up the size of the layout, I get this blank or white space at the bottom of the layout. It would appear that there is something going on with the margins but I can't seem to figure it out.

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/botHand3"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/botTile3_0"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_4"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_5"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_6"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_7"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_8"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_9"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

I don't ever get the error when the LinearLayout is horizontal. Also, when I set the gravity to center, the whitespace is divided between the top and bottom of the layout.

That's probably happening because your last button is bigger than the space left in the activity view, so it can't render properly if you do not set your view as ScrollView.

So, in this case you should define a ScrollView type to your view OR reduce your button heights to correctly fill your view actual height.

However, if you just want to add the ScrollView, try the following as example:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

    <LinearLayout
        android:id="@+id/botHand3"
        android:layout_width="50dp"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <Button
            android:id="@+id/botTile3_0"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_3"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_4"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_5"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_6"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_7"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_8"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginBottom="-3dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/botTile3_9"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginTop="-3dp"
            android:layout_weight="1" />
    </LinearLayout>

    </ScrollView>

</RelativeLayout>

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM