简体   繁体   中英

aligning button to bottom within linearlayout parent with minheight

I have a LinearLayout parent which contains two LinearLayout child of which second child contains Button which should be place at bottom of the parent view. I'm using android:layout_gravity="center_horizontal|bottom" on second child LinearLayout

Still I'm getting the output as shown below: 截图

I tried using RelativeLayout as a parent but that is making dialog height as big as device screen size which I don't want.

If I remove minHeight attribute from LinearLayout parent, then parent wraps to height less than minHeight

Any hep appreciated.

Here's my code for reference:

<?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="wrap_content"
    android:background="@drawable/background_dialog"
    android:minHeight="200dp"
    android:minWidth="400dp"
    android:orientation="vertical" >

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/ivDialogImage"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="15dp"
                android:src="@drawable/icon_cfs_activated" />

            <TextView
                android:id="@+id/tvDialogMessage"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:gravity="center_vertical"
                android:minHeight="80dp"
                android:paddingRight="5dp"
                android:textColor="@color/text_color"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" >

        <Button
            android:id="@+id/btndialogOk"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/background_button_selector"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="@string/alert_ok"
            android:textColor="@color/text_color" />
    </LinearLayout>

</LinearLayout>

android:layout_gravity defines the gravity for the Layout itself inside it's parent view.

What you should use to apply gravity for child layouts is:

android:gravity="center_horizontal|bottom"

Also, your bottom LinearLayout height set to wrap_content the button will fit it not matter what you did with the gravity. add a background color to the LinearLayout to observe that.

I think what you are looking for, for your bottom layout is:

<LinearLayout
        android:id="@+id/ll_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" >

This will force your bottom layout to fill the rest of the Parent LinearLayout

Here's your answer i have just edited your code change your drawables and colors

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



<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="15dp"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <ImageView
        android:id="@+id/ivDialogImage"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="15dp"
        android:src="@drawable/cam" />

    <TextView
        android:id="@+id/tvDialogMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center_vertical"
        android:minHeight="80dp"
        android:paddingRight="5dp"
        android:layout_toRightOf="@+id/ivDialogImage"
        android:textColor="@color/red"
        android:textSize="15sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
       >


    </RelativeLayout>

    <Button
        android:id="@+id/btndialogOk"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/scan"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:text="ok"
        android:textColor="@color/red"


        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>





</RelativeLayout>

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