如何以百分比设置相对布局的高度和宽度

时间:2016-10-22 08:36:21

标签: android android-layout

我的LinearLayout有一定的方格空间。在此LinearLayout中,我有RelativeLayout,其高度和宽度为100dp。此RelativeLayout具有圆形可绘制形状。我想将RelativeLayout的高度和宽度设置为其父LinearLayout可用总空间的60%。有没有办法做到这一点?

这是我正在设计的布局 enter image description here

这是我的一个圆形视图的xml代码片段

 <RelativeLayout
            android:id="@+id/rl_hatchback"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"

            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/rl_hatchback_circle"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@drawable/circle_red"

                >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_hatchback"
                    android:layout_centerInParent="true"
                    />
            </RelativeLayout>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/rl_hatchback_circle"
                    android:text="Hatchback"
                    style="@style/CircularTitle"
                    />
            </LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可以使用PercentRelativeLayout或ConstraintLayout来减少嵌套视图的数量。 供参考:

PercentRelativeLayout: https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

<强> ConstraintLayout: https://developer.android.com/training/constraint-layout/index.html