Android的:如何均匀内RelativeLayout的空间的分量? [英] Android: how evenly space components within RelativeLayout?
问题描述
我试图组织3 LinearLayouts,使得一个是调整到左边,一到右边,而第三是在两者之间具有空白空间两侧等量。我想我在一个RelativeLayout的包装这些做到这一点。然后设置一个布局alignParentLeft和第二布局alignParentRight。但对于第3的布局,我想设置layout_marginLeft =(RelativeLayout.width - (layout1.width + layout2.width + layout3.width))/ 2。不过,我看不出如何做到这一点的XML。有什么建议?
*编辑。根据建议,我现在用的是LinearLayout中。这更接近的工作,但在3布局不均匀间隔,和右布局不右对齐。下面是我的xml:
<的LinearLayout
机器人:ID =@ + ID /工具栏
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =底
机器人:paddingTop =4dip
机器人:paddingBottom会=4dip
机器人:后台=#5000
机器人:能见度=有形与GT;
<的LinearLayout
机器人:方向=横向
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT>
<的ImageButton
机器人:ID =@ + ID /打开
机器人:SRC =@可绘制/打开
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< / ImageButton的>
< / LinearLayout中>
<的LinearLayout
机器人:方向=横向
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT>
<的ImageButton
机器人:ID =@ + ID / $ P $光伏
机器人:SRC =@可绘制/ $ P $光伏
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< / ImageButton的>
<的TextView
机器人:ID =@ + ID /总页数
机器人:文本=0 0
机器人:layout_gravity =中心
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=#FFFFFFFF>
< / TextView的>
<的ImageButton
机器人:ID =@ + ID /下一个
机器人:SRC =@可绘制/下一个
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< / ImageButton的>
< / LinearLayout中>
<的LinearLayout
机器人:方向=横向
机器人:layout_gravity =右
机器人:layout_width =WRAP_CONTENT
机器人:layout_weight =1
机器人:layout_height =WRAP_CONTENT>
<的ImageButton
机器人:ID =@ + ID / zoomout
机器人:SRC =@可绘制/ zoomout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< / ImageButton的>
<的ImageButton
机器人:ID =@ + ID / zoomin
机器人:SRC =@可绘制/ zoomin
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT>
< / ImageButton的>
< / LinearLayout中>
< / LinearLayout中>
使用的LinearLayout而不是RelativeLayout的,并使用layout_weight属性。 您可能需要设置layout_width为0dip,然后设置权重为类似0.33
编辑: 是的,你可以使用一个重量1或小于1的任何其他相等的权重。 例如
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=横向
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的LinearLayout
机器人:方向=垂直
机器人:layout_width =0dip
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=串1试验......
/>
< / LinearLayout中>
<的LinearLayout
机器人:方向=垂直
机器人:layout_width =0dip
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =center_horizontal
机器人:文本=2串测试......
/>
< / LinearLayout中>
<的LinearLayout
机器人:方向=垂直
机器人:layout_width =0dip
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
>
<的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =右
机器人:文本=3串测试......
/>
< / LinearLayout中>
< / LinearLayout中>
I am trying to organize 3 LinearLayouts, such that one is justified to the left, one to the right, and the 3rd is between the two with equal amount of blank space on either side. I think I wrap these in a RelativeLayout to accomplish this. Then set one layout to alignParentLeft and the 2nd layout to alignParentRight. But for the 3rd layout, I would like to set layout_marginLeft = (RelativeLayout.width - (layout1.width + layout2.width + layout3.width))/2. Though, I don't see how to do this in xml. Any suggestions?
*Edit. Based on suggestions, I am now using a LinearLayout. This is closer to working, but the 3 Layouts are not evenly spaced, and the right layout is not right justified. Below is my xml:
<LinearLayout
android:id="@+id/toolbar"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingTop="4dip"
android:paddingBottom="4dip"
android:background="#50000000"
android:visibility="visible">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/open"
android:src="@drawable/open"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/prev"
android:src="@drawable/prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<TextView
android:id="@+id/totalpages"
android:text="0 of 0"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF">
</TextView>
<ImageButton
android:id="@+id/next"
android:src="@drawable/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/zoomout"
android:src="@drawable/zoomout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
<ImageButton
android:id="@+id/zoomin"
android:src="@drawable/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageButton>
</LinearLayout>
</LinearLayout>
Use a LinearLayout instead of the RelativeLayout and use layout_weight attribute. You might need to set the layout_width to 0dip and then set the weight to something like 0.33
Edit: Yes you can use a weight 1 or any other equal weight below 1. example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string 1 for test...."
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="string 2 for test...."
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="string 3 for test...."
/>
</LinearLayout>
</LinearLayout>
这篇关于Android的:如何均匀内RelativeLayout的空间的分量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!