RelativeLayout layout_centerInParent and layout_below issue

本文讨论了在使用RelativeLayout进行布局时遇到的问题,特别是当设置layout_centerInParent为true且使用layout_below属性时,子视图未能正确显示的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a problem with laying out a Relative Layout. Here is the test code

<RelativeLayout
    android:background="@drawable/bg_controls"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/controls_layout"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/controls_layout" />

</RelativeLayout>

The result of that is enter image description here

If I remove android:layout_centerInParent="true" everything works OK. Why does RelativeLayout fails to do that otherwise?

UPD: the intended result is that a second button appears under the LinearLayout.

share | improve this question
 
 
What is the intended result? –   prolink007  May 16 '11 at 19:54
 
What exactly are you trying to make it do? –   FoamyGuy  May 16 '11 at 19:54
 
the intended result is that a second button appears under the LinearLayout. I thought that was obvious... –  Kostia Dombrovsky  May 16 '11 at 19:56
 
by under the linearlayout, do you mean below the linearLayout? –   PravinCG  May 16 '11 at 19:57
 
Can you be a little more specific regarding the image itself instead of the layouts? –   prolink007  May 16 '11 at 19:58

3 Answers

Are you using API level 3 (1.5)? RelativeLayouts are very quirky for 1.5. Giving your buttons an id is a good idea.

share | improve this answer
 
 
I'm using 1.6 10chars –   Kostia Dombrovsky  May 16 '11 at 20:01

Have you tried running this on an actual device? I got this problem when previewing the layout in eclipse, but when I ran it on a device, it worked as intended

share | improve this answer
 

I was having a very similar issue, where I had a centered View that was designated layout_centerInParent, and all my other Views were relative to that, but some of the Views were not rendering properly in the editor or on the device.

I found that my centered View itself, designated layout_centerInParent, still had to be relative to something else (as unintuitive as that sounds). So I made it layout_below something that was on top of it, even though that View on top of it was not aligned to anything except being layout_top of the same thing below. But once I did this, the whole UI snapped into place :)

share | improve this answer
 







































































































































http://stackoverflow.com/questions/6022553/relativelayout-layout-centerinparent-and-layout-below-issue





<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.example.myapplication.GameView android:id="@+id/gameView" android:layout_width="match_parent" android:layout_height="match_parent"/> <TextView android:id="@+id/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:paddingTop="10dp" android:text="0" android:textColor="@color/black" android:textSize="18dp" /> <RelativeLayout android:visibility="gone" android:id="@+id/relative" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/introdution" > <TextView android:id="@+id/scoreText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:backgroundTint="@color/black" android:text="分数:" android:textSize="18dp" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:backgroundTint="#0068B5" android:text="重新开始" android:textSize="18dp" /> </RelativeLayout> <RelativeLayout android:id="@+id/introdution" android:layout_width="300dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/shuoming"> <Button android:id="@+id/btn_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:backgroundTint="#0068B6" android:text="开始游戏" android:textSize="18dp" /> </RelativeLayout> </RelativeLayout>解释这段布局文件代码
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值