如何设置子布局的高度和宽度以适应父布局?

How to set height and with of child layout to fit within parent layout?

我是 android 的新手,正在尝试使用 xml 作为背景制作 circular layouts。现在我有一个父相对布局,它有一个子相对布局。父级相对布局的背景 xml 带有圆角半径,并显示为圆形。现在内部相对 layout/child 布局也必须继承这个并且是一个圆圈吧?但它不是!子布局的高度和宽度为 match_parent & match_parent。那么如何让子布局的高度和宽度适合父布局的圆圈?

  <RelativeLayout
                    android:layout_marginTop="12dp"
                    android:layout_below="@+id/view10"
                    android:layout_centerHorizontal="true"
                    android:layout_width="52dp"
                    android:gravity="center"
                    android:background="@drawable/dutycirclebackground"
                    android:layout_height="52dp">

                    <RelativeLayout
                        android:visibility="visible"
                        android:layout_width="match_parent"
                        android:gravity="center"
                        android:layout_height="match_parent">
      </RelativeLayout>

</RelativeLayout>

这是背景xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#cc5228"/>
    <corners
        android:bottomRightRadius="25dp"
        android:bottomLeftRadius="25dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp"/>
</shape>

在这里,如果我为内部布局设置背景颜色并检查输出,我得到 square layoutparent is a circle.

提前致谢!

你可以试试这个希望对你有帮助..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:auto="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_below="@+id/view10"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:layout_marginTop="12dp"
android:background="@drawable/dutycirclebackground"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
    android:layout_width="52dp"
    android:layout_height="52dp"
    android:layout_centerHorizontal="true"
    android:layout_margin="10dp"
    android:layout_marginTop="12dp"
    android:background="@drawable/dutycirclebackground"
    android:gravity="center"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:gravity="center" />
   </RelativeLayout>
</RelativeLayout>

那样做
  android:radius="250dp"

改为所有角半径:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" android:padding="10dp">
    <solid android:color="#cc5228"/>
    <corners
        android:radius="250dp" />
</shape>

首先,如果您的 parent 布局是圆形的,这并不意味着您的 child 也是圆形的。

其次,在你的情况下你的 parent 也不是圆形而是矩形,你的 drawable 只隐藏它的角落并向你展示另一部分。这就是为什么,当您使用匹配 parent 或其他东西时,它会给您方形而不是圆形。

在 child 而不是 parent 中使用你的 drawable。

或者您可以使用 来达到方内圆的目的