我有如下布局。现在,我不想将相对布局的宽度设置为固定的 240 dp。我想将相对布局的宽度设置为屏幕宽度的 1/3。是否可以在 xml 文件中执行此操作。如果不可能,我怎样才能使用 java 代码实现它?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fullscreen"
style="@style/translucent">
<RelativeLayout
android:layout_width="240dp"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="center"
android:background="#88000000"
android:id="@+id/sidebar">
</RelativeLayout>
</FrameLayout>
最佳答案
在父级中使用 weightsum="3"
,在子级中使用 layout_weight=1
。 Take a look a this reference
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fullscreen"
style="@style/translucent"
android:orientation="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:gravity="center"
android:background="#88000000"
android:id="@+id/sidebar"
android:layout_weight="1"
>
</RelativeLayout>
<!-- other views, with a total layout_weight of 2 -->
</LinearLayout>
关于android - 将relativelayout的宽度设置为屏幕宽度的1/3?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21225213/