[英]Set the width of relativelayout 1/3 the width of the screen?
I have a layout like below. 我有一个如下的布局。 Now, I don't want to set the width of relative layout to fixed 240 dp.
现在,我不想将相对布局的宽度设置为固定240 dp。 I want to set the width of the relative layout to 1/3 the width of the screen.
我想将相对布局的宽度设置为屏幕宽度的1/3。 Is it possible to do that in the xml file.
是否可以在xml文件中执行此操作。 If it is impossible, how can I achieve that using java code ?
如果不可能,我怎么能用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>
use weightsum="3"
in the parent and layout_weight=1
in the child. 在父级中使用
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>
You have to use a LinearLayout
to get the width of a view to be a third of its parentview. 您必须使用
LinearLayout
将视图的宽度设置为其父视图的三分之一。
Something like: 就像是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:background="#88000000">
</RelativeLayout>
<ViewGroup
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2">
</ViewGroup>
</LinearLayout>
The key bit is the ratio of the layout_weights. 关键位是layout_weights的比率。 The documentation is pretty good.
文档非常好。
A LinearLayout
with a android:layout_orientation="horizontal"
is what you want, along with weights. 带有
android:layout_orientation="horizontal"
的LinearLayout
就是你想要的,还android:layout_orientation="horizontal"
重。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
...all your stuff... />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.