android 相对布局高度无法正常工作
android relativelayout height not working corretly
我希望 android:text="30" textview 位于 Relativelayout 的中心位置。
但 android:layout_height="match_parent" 无法正常工作。
看起来像 "wrap_content".
我什么都试过了
我发现再添加一个 Relativelayout 并且可以正常工作。
- 文本“30”如何移动中心位置
- 为什么 match_parent 不起作用?
为什么再添加一个 Relativelayout 就可以正常工作了?
<androidx.core.widget.NestedScrollView
android:id="@+id/product_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="56dp"
android:background="@color/productGridBackgroundColor"
android:elevation="8dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="24dp"
android:paddingTop="16dp">
<com.google.android.material.card.MaterialCardView
style="@style/Widget.MaterialComponents.CardView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/mtrl_card_spacing"
android:layout_marginTop="@dimen/mtrl_card_spacing"
android:layout_marginRight="@dimen/mtrl_card_spacing"
android:minHeight="200dp">
**<!--height = match_parent not working-->**
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_card_title_level"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="30"/>
</RelativeLayout>
<!--**if this code added then work perfectly**
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"/>
-->
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
删除相对布局并添加 android:layout_gravity="center
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="30"/>
我希望 android:text="30" textview 位于 Relativelayout 的中心位置。
但 android:layout_height="match_parent" 无法正常工作。 看起来像 "wrap_content".
我什么都试过了 我发现再添加一个 Relativelayout 并且可以正常工作。
- 文本“30”如何移动中心位置
- 为什么 match_parent 不起作用?
为什么再添加一个 Relativelayout 就可以正常工作了?
<androidx.core.widget.NestedScrollView android:id="@+id/product_grid" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="56dp" android:background="@color/productGridBackgroundColor" android:elevation="8dp" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:clipChildren="false" android:clipToPadding="false" android:orientation="vertical" android:padding="24dp" android:paddingTop="16dp"> <com.google.android.material.card.MaterialCardView style="@style/Widget.MaterialComponents.CardView" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_marginLeft="@dimen/mtrl_card_spacing" android:layout_marginTop="@dimen/mtrl_card_spacing" android:layout_marginRight="@dimen/mtrl_card_spacing" android:minHeight="200dp"> **<!--height = match_parent not working-->** <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/home_card_title_level"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="30"/> </RelativeLayout> <!--**if this code added then work perfectly** <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"/> --> </com.google.android.material.card.MaterialCardView> </LinearLayout> </androidx.core.widget.NestedScrollView>
删除相对布局并添加 android:layout_gravity="center
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="30"/>