我可以使用layout_weight来定位一个RelativeLayout的? [英] Can I use layout_weight to position a RelativeLayout?
问题描述
我一直在试图整天得到这个工作,我认为,我可以使用 RelativeLayout的
I've been trying all day to get this to work and i think that I can use RelativeLayout
android:layout_weight="0.3"
放置在右侧的屏幕,而不是居中但30%从上往下的三个按钮。
to place three buttons on the right side of the screen, not centered but 30% down from the top.
这是可能的,如果是的话我怎么办呢?
Is this possible and if so how do I do it?
下面是我的XML,显示下方对方,但在右上角的位置三个按钮:
The following is my XML that shows three buttons underneath each other but in top right position:
</RelativeLayout>
<LinearLayout
android:id="@+id/rightRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:orientation="vertical"
>
<ImageButton
android:id="@+id/btn_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="A"
android:src="@drawable/drawer_1"
android:background="@android:color/transparent"
/>
<ImageButton
android:id="@+id/btn_B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:text="B"
android:src="@drawable/drawer_1"
android:background="@android:color/transparent"
android:layout_below="@+id/btn_A"
/>
<ImageButton
android:id="@+id/btn_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="C"
android:src="@drawable/drawer_1"
android:background="@android:color/transparent"
android:layout_below="@+id/btn_B"
/>
</LinearLayout>
[更新]增加了最终的工作版本的人谁需要它
[UPDATE] Added final working version for people who need it
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/InnerRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="@+id/btn_newpen_drawtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pen"
/>
<EditText
android:id="@+id/etx_addtext_drawtext"
android:layout_width="fill_parent"
android:layout_toLeftOf="@+id/btn_delete_pen"
android:layout_toRightOf="@+id/btn_newpen_drawtext"
android:layout_height="wrap_content"
android:singleLine="true"
android:text=""
/>
<Button
android:id="@+id/btn_delete_pen"
android:layout_toLeftOf="@+id/btn_save_drawtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Del"
/>
<Button
android:id="@+id/btn_save_drawtext"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
/>
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rightLinerLayoutL0"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/RelativeLayout1"
android:weightSum="1.0"
android:layout_alignParentRight="true"
android:orientation="vertical"
>
<LinearLayout android:layout_height="0dp"
android:id="@+id/rightLinerLayoutL1"
android:layout_weight="0.15"
android:layout_width="0dp">
</LinearLayout>
<LinearLayout android:layout_height="0dp"
android:orientation="vertical"
android:id="@+id/rightLinerLayoutL2"
android:layout_weight="0.85"
android:layout_width="wrap_content">
<ImageButton android:text="Button_A"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:id="@+id/btn_A"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:src="@drawable/drawer"
android:onClick="btnAListener">
</ImageButton>
<ImageButton android:text="Button_B"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/btn_B"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:src="@drawable/drawer"
android:onClick="btnBListener">
</ImageButton>
<ImageButton android:text="Button_C"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:id="@+id/btn_C"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:src="@drawable/drawer"
android:onClick="btnCListener">
</ImageButton>
</LinearLayout>
</LinearLayout >
</RelativeLayout>
推荐答案
栈2的LinearLayout(L1,L2)为0.3和0.7的权重的部分(和0dp的高度)内的LinearLayout(L0)与垂直方向和总重量1.0。把你的按钮在0.7加权布局(L2)。这会给你30%以上的按键间距。
Stack 2 LinearLayout (L1, L2) sections with weights of 0.3 and 0.7 (and heights of 0dp) inside a LinearLayout (L0) with vertical orientation and total weight of 1.0. Put your buttons in the 0.7 weighted layout (L2). That will give you your spacing of 30% above the buttons.
您可以然后将含有的LinearLayout(L0)内的RelativeLayout的(R0)和地点L0相对于父(R0)的权利,这将使整个事情在屏幕右侧的。
You can then place the containing LinearLayout (L0) inside a RelativeLayout (R0) and place L0 relative to the right of the parent (R0), which will position the entire thing on the right side of the screen.
编辑:我的版本是pretty的多少同米尔德在结束时,除了使用 RelativeLayout的
根,所以我可以把 layout_alignParentRight =真正的
,以确保它是齐平的权利。唯一的症结点,我发现是确保你第一次的LinearLayout有 FILL_PARENT
高度,使你的间隔确实是屏幕的30%。
My version is pretty much same as Milde in end, except using a root of RelativeLayout
so I could put the layout_alignParentRight="true"
to ensure it's flush to the right. The only sticking point I found was ensuring your first LinearLayout has height of fill_parent
so that your spacer really is 30% of screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="wrap_content" android:weightSum="1.0" android:layout_alignParentRight="true">
<LinearLayout android:layout_weight="0.3" android:layout_height="0dp" android:layout_width="0dp" />
<LinearLayout android:layout_weight="0.7" android:layout_height="0dp" android:layout_width="wrap_content" android:orientation="vertical">
<Button android:text="Button 1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:text="Button 2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Button android:text="Button 3" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
这篇关于我可以使用layout_weight来定位一个RelativeLayout的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!