首页 文章

如何在相对布局中将Button对齐父级底部?

提问于
浏览
-1

我有简单的布局,包含ImageView,TextView和Button .

我想要实现的是Imageview和TextView位于父级内部的单独RelativeLayout中,而Button位于Imageview和TextView下但它应该对齐父级底部,但它不会像那样工作 . 它没有与底部对齐,而是在上面的底部父级和相对布局之间悬停 .

怎么做到这一点?

下面是它应该如何显示的图片表示:

enter image description here

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

enter image description here

整个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>

4 回答

  • 0
    `<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>`
    
  • 1

    试试这个,

    <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">
    ......
    

    其他的事情就是这样

  • 0

    从最顶层 RelativeLayout 删除 android:gravity="center" . 这会导致相对布局的所有子节点对齐居中,如果它在 LinearLayout

  • 0

    添加你的xml按钮

    android:layout_below="@+id/order_list_footer_layout"
    

相关问题