相对布局权重
在布局资源 XML 中,我有 3 个位于主相对布局内的相对布局.视图将垂直显示.这 3 个 RelativeLayout() 彼此相邻设置,我希望它们填满整个屏幕,无论屏幕大小如何.我的布局视图:
In a layout resource XML, I have 3 RelativeLayout(s) which are inside a main RelativeLayout. The view will be shown vertically. These 3 RelativeLayout() are set next to each other, and I want them to fill the whole screen, doesnt matter what will be the screen size. My, layout view:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backg"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="@dimen/top_mr_image"
android:src="@drawable/temp" />
<RelativeLayout
android:id="@+id/r1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/imageView1"
android:layout_marginLeft="10dp"
android:background="@drawable/r1bg"
android:layout_weight="1"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="@dimen/txt_mr_right"
android:layout_marginRight="@dimen/txt_mr_right"
android:layout_marginTop="39dp"
android:text="S"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/textView1"
android:layout_marginLeft="@dimen/txt_mr_right"
android:layout_marginRight="@dimen/txt_mr_right"
android:text="T"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/r2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/r1"
android:layout_toRightOf="@+id/r1"
android:layout_weight="1" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/r3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/r2"
android:layout_toRightOf="@+id/r2"
android:layout_weight="1" >
</RelativeLayout>
我为每个 relativeLayout 设置了 weight=1
和 layout_width=0dp
并且这种技术适用于按钮,我认为relativeLayout 也是如此,似乎我的想法是错误的.有什么想法吗?
I set weight=1
and layout_width=0dp
for each relativeLayout and this technique works with buttons, I thought the same will be with relativeLayout, seems my thoughts were wrong. Any idea?
UPD1:我添加了我想要的图片
UPD1: I have added an image of what I would like to have
RelativeLayout
不关注android:layout_weight
.(这是 LinearLayout.LayoutParams
的属性,但不是RelativeLayout.LayoutParams
.)
RelativeLayout
does not pay attention to android:layout_weight
. (That's a property of LinearLayout.LayoutParams
, but not of RelativeLayout.LayoutParams
.)
您应该能够通过更简单的视图层次结构获得所需的布局.不清楚您要做什么,因为最后两个 RelativeLayout
是空的.如果您需要一个纯粹的垂直组织,我建议使用 LinearLayout
而不是 RelativeLayout
.
You should be able to get the layout you want with a much simpler view hierarchy. It's not clear what you are trying to do, since the last two RelativeLayout
s are empty. If you need a purely vertical organization, I'd suggest using LinearLayout
instead of RelativeLayout
.
EDIT 根据您的编辑,您似乎想要三个复合视图的水平布局,每个视图都可点击.我认为类似以下内容会起作用:
EDIT Based on your edit, it looks like you want a horizontal layout of three compound views, each one clickable. I think something like the following will work:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- First column -->
<LinearLayout
android:id="@+id/firstColumn"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="..." />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="text 1"
. . . />
</LinearLayout>
<!-- Second column -->
<LinearLayout . . . >
. . .
</LinearLayout>
</LinearLayout>
如果按钮的内容不正确,您可以用 RelativeLayout
替换二级 LinearLayout
视图,如果这有助于更好地组织布局.
If the contents of the buttons aren't correct, you can replace the second-level LinearLayout
views with RelativeLayout
if that helps organize the layout better.