Android - AlignParentBottom 占用了所有的 Free Space
Android - AlignParentBottom takes up all the Free Space
我正在尝试在布局底部对齐 button
。但问题是,每当我使用 layout_alignParentBottom="true"
时,它都会占用所有免费 space,正如您在下面的屏幕截图中看到的那样。
这是XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_transparent"
android:orientation="vertical"
android:padding="5dp"
android:theme="@style/Theme.MaterialComponents.DayNight"
tools:context=".Activities.DisplayProductDetails">
<ImageView
android:id="@+id/display_ProductImage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/material"
android:layout_below="@+id/display_ProductImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/ic_rectangle"
android:padding="20dp"
android:rotationX="20"
app:cardCornerRadius="20dp"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/display_ProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="@font/raleway"
android:gravity="center"
android:padding="10dp"
android:text="@string/product_name"
android:textColor="#eee"
android:textSize="22sp" />
<TextView
android:id="@+id/display_ProductDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/display_ProductName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center"
android:padding="10dp"
android:text="@string/product_description"
android:textColor="#eee"
android:textSize="18sp" />
<TextView
android:id="@+id/display_ProductPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/display_ProductDesc"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="15dp"
android:gravity="center"
android:padding="10dp"
android:text="@string/product_price"
android:textColor="#eee"
android:textSize="18sp" />
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="@+id/enb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/display_ProductPrice"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:gravity="center"
app:backGroundColor="@color/buttonColor"
app:finalNumber="50"
app:initialNumber="1"
app:textColor="#eee" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<Button
android:id="@+id/display_AddtoCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/material"
android:layout_alignParentBottom="true"
android:background="@color/buttonColor"
android:fontFamily="@font/raleway"
android:gravity="center"
android:text="@string/add_to_cart"
android:textColor="#eee"
android:textSize="20sp" />
</RelativeLayout>
此行导致 Button
的上边缘与 MaterialCardView
的下边缘对齐
android:layout_below="@+id/material"
并且此行导致 Button
的底边与父级
的底边对齐
android:layout_alignParentBottom="true"
layout_height="wrap_content"
在这种情况下不是 considered/calculated,据我记得它甚至可能是 0px。所以一切看起来都像您在 XML...
中设计的
如果你想在底部对齐按钮只需使用 layout_alignParentBottom="true"
并删除 layout_below="@+id/material"
(无论如何它都会在下面)
我正在尝试在布局底部对齐 button
。但问题是,每当我使用 layout_alignParentBottom="true"
时,它都会占用所有免费 space,正如您在下面的屏幕截图中看到的那样。
这是XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_transparent"
android:orientation="vertical"
android:padding="5dp"
android:theme="@style/Theme.MaterialComponents.DayNight"
tools:context=".Activities.DisplayProductDetails">
<ImageView
android:id="@+id/display_ProductImage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:scaleType="centerCrop" />
<com.google.android.material.card.MaterialCardView
android:id="@+id/material"
android:layout_below="@+id/display_ProductImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/ic_rectangle"
android:padding="20dp"
android:rotationX="20"
app:cardCornerRadius="20dp"
android:layout_centerHorizontal="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/display_ProductName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:fontFamily="@font/raleway"
android:gravity="center"
android:padding="10dp"
android:text="@string/product_name"
android:textColor="#eee"
android:textSize="22sp" />
<TextView
android:id="@+id/display_ProductDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/display_ProductName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:gravity="center"
android:padding="10dp"
android:text="@string/product_description"
android:textColor="#eee"
android:textSize="18sp" />
<TextView
android:id="@+id/display_ProductPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/display_ProductDesc"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="15dp"
android:gravity="center"
android:padding="10dp"
android:text="@string/product_price"
android:textColor="#eee"
android:textSize="18sp" />
<com.cepheuen.elegantnumberbutton.view.ElegantNumberButton
android:id="@+id/enb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/display_ProductPrice"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:gravity="center"
app:backGroundColor="@color/buttonColor"
app:finalNumber="50"
app:initialNumber="1"
app:textColor="#eee" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<Button
android:id="@+id/display_AddtoCart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/material"
android:layout_alignParentBottom="true"
android:background="@color/buttonColor"
android:fontFamily="@font/raleway"
android:gravity="center"
android:text="@string/add_to_cart"
android:textColor="#eee"
android:textSize="20sp" />
</RelativeLayout>
此行导致 Button
的上边缘与 MaterialCardView
android:layout_below="@+id/material"
并且此行导致 Button
的底边与父级
android:layout_alignParentBottom="true"
layout_height="wrap_content"
在这种情况下不是 considered/calculated,据我记得它甚至可能是 0px。所以一切看起来都像您在 XML...
如果你想在底部对齐按钮只需使用 layout_alignParentBottom="true"
并删除 layout_below="@+id/material"
(无论如何它都会在下面)