【问题标题】:RelativeLayout position problems相对布局位置问题
【发布时间】:2012-08-06 21:50:11
【问题描述】:

我有一个layout,其中包含 4 个水平的 LinearLayouts,每个里面有 3 个 ImageButtons

现在我想将那些LinearLayout 转换为RelativeLayout,因为我需要添加更多元素并且我必须使用toRightOfalignParentTop 等。

这是我用来进行此更改的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/bugbox_wall_port" >
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical" >
        <Button
            android:id="@+id/writeNewMsg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="New" />
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/spyMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgspy" />
            <ImageButton
                android:id="@+id/playerMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/msgplyr"
                android:layout_toRightOf="@id/spyMsgs" />
            <ImageButton
                android:id="@+id/allyMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgally"
                android:layout_toRightOf="@id/playerMsgs" />
        </RelativeLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/battleMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgbatt" />
            <ImageButton
                android:id="@+id/skynetMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/msgnet" />
            <ImageButton
                android:id="@+id/transportMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgtrans" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/expeditionMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgexp" />
            <ImageButton
                android:id="@+id/newsMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/msgnews" />
            <ImageButton
                android:id="@+id/buildingMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgbuild" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/allMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgall" />
            <ImageButton
                android:id="@+id/binMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@drawable/msgbin" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

但我不知道为什么,RelativeLayout 有问题。

这是我以图形方式解释我的问题的图像:

http://img811.imageshack.us/img811/5479/stackes.jpg

这让我发疯了。我不知道为什么会这样。我之前用过RelativesLayout,没有问题。

所以我的问题是:为什么会这样?然后我该如何解决这个错误?

提前谢谢你!

【问题讨论】:

标签: android android-layout android-relativelayout


【解决方案1】:

试试这个。我注释掉了底部的线性布局。如果相对布局按您希望的方式工作,那么您可以相应地调整其他线性。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:background="@drawable/bugbox_wall_port" >
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical" >
        <Button
            android:id="@+id/writeNewMsg"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="New" />
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/spyMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher" />
            <ImageButton
                android:id="@+id/playerMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/ic_launcher"
                android:layout_toRightOf="@id/spyMsgs" />
            <ImageButton
                android:id="@+id/allyMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_launcher"
                android:layout_toRightOf="@id/playerMsgs" />
        </RelativeLayout>
        <!-- <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/battleMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgbatt" />
            <ImageButton
                android:id="@+id/skynetMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/msgnet" />
            <ImageButton
                android:id="@+id/transportMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgtrans" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/expeditionMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgexp" />
            <ImageButton
                android:id="@+id/newsMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@drawable/msgnews" />
            <ImageButton
                android:id="@+id/buildingMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgbuild" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp" >
            <ImageButton
                android:id="@+id/allMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/msgall" />
            <ImageButton
                android:id="@+id/binMsgs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@drawable/msgbin" />
        </LinearLayout> -->
    </LinearLayout>
</ScrollView>

【讨论】:

    【解决方案2】:

    解决了。我正在以编程方式为视图设置 ID,但我没有意识到......

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多