RelativeLayout的为ListView项 [英] RelativeLayout as listview item
问题描述
考虑以下RelativeLayout的是列表视图项:
Consider following RelativeLayout as list view item:
<?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="wrap_content">
<TextView
android:id="@+id/bigfoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="bigfoo"
android:textSize="60sp"/>
<TextView
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bigfoo"
android:layout_centerVertical="true"
android:text="foo"/>
<TextView
android:id="@+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/foo"
android:layout_alignLeft="@id/foo"
android:text="bar"/>
</RelativeLayout>
与 hierarchyviewer
(与Android JB / API 17设备)栏
得0的高度。调查后
After investigating it with hierarchyviewer
(on device with Android JB/API 17) bar
gets 0 height.
编辑:预期的结果:
Expected result:
问:什么是这种相对的布局行为的解释,
如何解决布局来实现布局,满足要求:富
是 bigfoo
的中间(垂直)和栏
超过富
?
Question: What is the explanation of such relative layout behavior, and
how to fix the layout to achieve layout, that meets requirements: foo
is in the middle (vertically) of bigfoo
and bar
is above foo
?
推荐答案
我看到两个选项:
选项1 ,没有嵌套(这样的酒吧是在布局的顶部,但你可以设置一个最高保证金以降低它有点如果您使用的布局工程):
Option 1, no nesting (that way bar is on the top of the layout, but you can set a top margin to lower it a bit if that works with your use of the layout):
<?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="wrap_content">
<TextView
android:id="@+id/bigfoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="bigfoo"
android:textSize="60sp"/>
<TextView
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bigfoo"
android:layout_centerVertical="true"
android:text="foo"/>
<TextView
android:id="@+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bigfoo"
android:text="bar"/>
</RelativeLayout>
选项2 ,在第一个嵌套另一个RelativeLayout的:
Option 2, nesting another RelativeLayout in the first one :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/bigfoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="bigfoo"
android:textSize="60sp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/bigfoo"
android:layout_alignBottom="@id/bigfoo"
android:orientation="vertical"
android:layout_toRightOf="@id/bigfoo">
<TextView
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="foo" />
<TextView
android:id="@+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/foo"
android:text="bar" />
</RelativeLayout>
</RelativeLayout>
使用选项2,您应该做到正是你期待的结果,但在里面嵌套另一个布局的成本。如果UI的其余部分是光,它不应该是一个问题;)
With Option 2, you should achieve exactly the result you're expecting, but at the cost of nesting a layout inside another one. If the rest of the UI is light, it shouldn't be a problem ;)
修改:你能不能试试这个,请
EDIT : could you try this, please ?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/bigfoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="bigfoo"
android:textSize="60sp"/>
<TextView
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bigfoo"
android:layout_centerVertical="true"
android:text="foo"/>
<TextView
android:id="@+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/foo"
android:layout_alignLeft="@id/foo"
android:text="bar"/>
</RelativeLayout>
这是我所看到的,但我有麻烦解释为什么这个工程...
This is what I see, although I'd have trouble explaining why this works ...
这篇关于RelativeLayout的为ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!