如何设置相对布局的高度和宽度百分比
问题描述:
我对我的LinearLayout
有一定的方形空间。在此LinearLayout
我有一个RelativeLayout
与高度和宽度100dp
。这RelativeLayout
有一个圆形的可绘制的形状。我想将RelativeLayout
的高度和宽度设置为其母公司LinearLayout
可用总空间的60%。有没有办法做到这一点?如何设置相对布局的高度和宽度百分比
布局这是我的一个圆形视图
<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>
答
您可以使用PercentRelativeLayout或ConstraintLayout减少嵌套视图数的XML代码段。 仅供参考:
PercentRelativeLayout:https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html
ConstraintLayout: https://developer.android.com/training/constraint-layout/index.html
如果你想在百分比来设置,你可以根据你的百分比 –
1.有使用新的约束的布局和设置指南对于你想要达到的目标有很多答案,那么这个60%的身高究竟是什么意思呢? 2.您的布局非常糟糕,可能会影响您的应用性能。 3.一旦你回复第一点,我可以进一步帮助#1和# –
你能否建议我最好的方式来表示这种布局 –