簡體   English   中英

如何在相對布局中將按鈕與父底部對齊?

[英]How to align Button to parent bottom in Relative Layout?

我有包含 ImageView、TextView 和 Button 的簡單布局。

我想要實現的是 Imageview 和 TextView 位於以父級為中心的單獨 RelativeLayout 中,而 Button 位於 Imageview 和 TextView 下,但它應該與父級底部對齊,但它不會那樣工作。 它不與底部對齊,而是懸停在底部父級和上面的相對布局之間。

如何實現這一目標?

這是它應該是什么樣子的圖片表示:

在此處輸入圖片說明

如果我添加android:layout_below="@+id/order_footer_image_text_layout"

在此處輸入圖片說明

整個 XML(如果 recyclerview 為空,order_list_footer_layout 應該可見......這就是它們重疊的原因)

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/toolbar_round"
        android:orientation="horizontal">

        <include
            android:id="@+id/toolBarContent"
            layout="@layout/order_toolbar_layout" />

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/order_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolBar"
        android:layout_marginTop="8dp"
        android:background="@drawable/recyclerview_corners"
        android:clipToPadding="false"
        android:paddingTop="8dp"
        android:scrollbars="vertical" />

    <RelativeLayout
        android:id="@+id/order_list_footer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolBar"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/order_footer_image_text_layout"
            android:layout_centerInParent="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/order_footer_image"
                android:layout_width="@dimen/orderTabFooter_width"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:src="@drawable/image_order" />

            <TextView
                android:id="@+id/order_footer_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/order_footer_image"
                android:layout_marginEnd="50dp"
                android:layout_marginStart="50dp"
                android:layout_marginTop="-16dp"
                android:gravity="center_horizontal"
                android:text="@string/order_footer_label"
                android:textColor="@color/colorItemMinor"
                android:textSize="@dimen/food_list_top_row_font" />
        </RelativeLayout>

        <Button
            android:id="@+id/order_choice_button"
            android:layout_width="match_parent"
            android:layout_height="@dimen/order_button_height"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="@dimen/order_button_padding_TOP_BOTTOM"
            android:layout_marginEnd="@dimen/order_button_padding_START_END"
            android:layout_marginStart="@dimen/order_button_padding_START_END"
            android:layout_marginTop="@dimen/order_button_padding_TOP_BOTTOM"
            android:background="@drawable/order_button_background_void"
            android:text="Choose Food"
            android:textAppearance="@style/VoidLoginButtonTextAppearance" />

    </RelativeLayout>

</RelativeLayout>

從最頂部的RelativeLayout刪除android:gravity="center" 這會導致相對布局的所有子項居中對齊,如果它在LinearLayout

添加按鈕的xml

android:layout_below="@+id/order_list_footer_layout"
            `<RelativeLayout
                    android:id="@+id/order_list_footer_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <RelativeLayout
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:id="@+id/order_footer_image_text_layout"
                        android:layout_centerInParent="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/order_footer_image"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:layout_marginEnd="16dp"
                            android:layout_marginStart="16dp"
                            android:layout_marginTop="16dp"
                            android:src="@drawable/ic_launcher"/>

                        <TextView
                            android:id="@+id/order_footer_text"
                            android:layout_width="wrap_content"
                            android:layout_height="20dp"
                            android:layout_centerHorizontal="true"
                            android:layout_below="@id/order_footer_image"
                            android:gravity="center_horizontal"
                            android:text="test" />
                    </RelativeLayout>

                    <Button
                        android:id="@+id/order_choice_button"
                        android:layout_width="match_parent"
                        android:layout_height="50dp"
                        android:layout_alignParentBottom="true"`enter code here`
                        android:text="Button"/>

                </RelativeLayout>`

嘗試這個,

<RelativeLayout
android:id="@+id/order_list_footer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolBar"
android:layout_centerHorizontal="true"
android:gravity="center"  // remove this line
android:orientation="vertical">
......

其他事情就是這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM