Android RelativeLayout作为listview项
将以下RelativeLayout视为列表视图项:Android RelativeLayout作为listview项,android,android-listview,android-relativelayout,Android,Android Listview,Android Relativelayout,将以下RelativeLayout视为列表视图项: <?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
<?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设备上)对其进行调查后,bar
获得0高度
编辑:预期结果:
问题:如何解释这种相对布局行为,以及
如何固定版图来实现版式,符合要求:<代码> FoO 是在<代码> BigFoo的中间(垂直)和<代码> BAR >上面>代码> Foo < /P> < P>我看到2个选项:
选项1,无嵌套(该方式栏位于布局顶部,但如果您使用该布局,您可以设置顶部边距以将其降低一点):
选项2,在第一个RelativeLayout中嵌套另一个RelativeLayout:
<?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的其余部分是轻量级的,那么这应该不是问题;)
编辑:您能试试这个吗
<?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>
这就是我所看到的,虽然我很难解释为什么会这样。。。
它在android:layout_下方=“@id/foo”
而不是android:layout_上方=“@id/foo”
上工作真是荒谬。但问题仍然是为什么会这样+1@ShakeebShaheen:您的意思是,如果我将其更改为android:layout_below=“@id/foo”
,它实际上可以工作吗?是的,完全正确。我在Ide中复制了xml,并按照上面所说的进行了更改。它工作正常奇怪的是它工作正常。。。但它不应该,我也不明白为什么。我会的,但现在唯一剩下的事情,因为它不明显,就是拉取RelativeLayout源代码,附加到示例项目,并逐步调试它,以找出它为什么会这样。不幸的是,我现在没有时间。但我会保留这个问题-也许我会找时间这样做…选项1:不适用,因为bar
不是“高于”foo,我知道你的想法是稍微调整它的顶部边距,但它将取决于设备,不是吗?选项2:这实际上有点帮助,但我不想为每个列表项引入嵌套,我的实际布局有点复杂,为了解决这个问题,我去掉了不必要的部分。让我们看看是否有人提出了更好的解决方案。。。尽管如此,我还是会测试嵌套是否会影响性能,如果是,则尝试使布局的其余部分更轻;)我已经以类似的方式处理了这个问题,但我最感兴趣的是,为什么RelativeLayout在这个案例中表现得如此奇怪。我希望有人能说类似的话:“在本例中,onMesure()给出了0,因为……”,但无论如何,感谢您的努力!看看我编辑过的答案,也许Eclipse预览版快疯了,或者这个神奇地工作是因为一些奇怪的原因:-)实际上它工作了,但奇怪的是,如果我想在foo
下面添加一些东西,那是不可能的。尽管如此,我仍然对为什么相对布局在这种情况下表现得如此奇怪感兴趣?请对您的答案进行一些解释。
<?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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/bigfoo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="bigfoo"
android:textSize="60sp"/>
<TextView
android:id="@+id/bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/bigfoo"
android:text="bar"/>
<TextView
android:id="@+id/foo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bar"
android:layout_toRightOf="@id/bigfoo"
android:text="foo"/>
</RelativeLayout>