如何在相对布局中将按钮与父底部对齐?
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>
添加你的xml按钮
android:layout_below="@+id/order_list_footer_layout"
从最上面的 RelativeLayout
中删除 android:gravity="center"
。如果它在 LinearLayout
中,这会导致相对布局的所有子元素对齐中心
`<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">
......
其他的就是这样
我有一个包含 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>
添加你的xml按钮
android:layout_below="@+id/order_list_footer_layout"
从最上面的 RelativeLayout
中删除 android:gravity="center"
。如果它在 LinearLayout
`<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">
......
其他的就是这样