【问题标题】:How to set height and with of child layout to fit within parent layout?如何设置高度和子布局以适应父布局?
【发布时间】:2016-12-17 20:18:00
【问题描述】:

我是 android 新手,正在尝试使用 xml 作为背景制作circular layouts。现在我有一个父相对布局,它有一个子相对布局。父相对布局有一个带角半径的背景 xml,并显示为圆形。现在内部相对布局/子布局也必须继承这个并且是一个圆圈,对吗?但它不是!子布局的高度和宽度为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

提前致谢!

【问题讨论】:

  • 移除子布局中的 android:gravity="center"
  • @sasikumar,删除并检查,但仍然是一个正方形
  • @Sidharth MA 你在实际设备上运行过它吗?

标签: android android-layout geometry android-relativelayout


【解决方案1】:

照做

  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>

【讨论】:

    【解决方案2】:

    首先,如果你的父布局是圆形的,并不意味着你的孩子也是圆形的。

    其次,如果你的父母也不是圆形的,它是矩形的,你可以绘制的只是隐藏它的角并显示另一部分。这就是为什么,当你使用 match parent 或其他东西时,它会给你方形而不是圆形。

    在子级而非父级中使用您的可绘制对象。

    或者您可以使用Dileep Patel's answer 来实现圆内正方形

    【讨论】:

      【解决方案3】:

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

      <?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>
      

      【讨论】:

      • 非常感谢 dileep patel,它帮助了我!
      • 嘿,我两个都做不到,因为我的声誉低于 15 :)
      猜你喜欢
      • 2016-01-23
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多