【问题标题】:XML is crazy. It is not showing real position of elementsXML 很疯狂。它没有显示元素的真实位置
【发布时间】:2012-07-31 21:52:40
【问题描述】:

我的布局有问题。我有这个结构:

<ScrollView>
    <LinarLayout>
        <Button/>
        <RelativeLayout>
            <ImageButton/>
            <ImageButton/>
            <ImageButton/>
        </RelativeLayout>
    </LinarLayout>
</ScrollView>

我希望三个 ImageButtons 居中,一个并排,如下所示:

IMG1   IMG2   IMG3

为此,我使用android:gravity="center"RelativeLayout 中的元素居中,并使用android:layout_toRightOf="@+id/element"android:layout_toLeftOf="@+id/element" 按我想要的方式对元素进行排序。

当我在GraphicalLayout 选项上检查结果时,它显示的正是我想要的(我从来没有遇到过这个视图的问题,它总是显示我在XML 文件),但随后我编译并执行,一切都是无序的。我已经尝试了很多不同的方式来排序元素,但它正在做它想做的事情......这是我近两年的开发中第一次发生这种情况。

我也关闭了 Eclipse,但没有任何改变。

编辑以添加信息

我希望元素是这样的:

IMG1 IMG2 IMG3

在 GraphicalLayout 中一切正常,但在模拟器中,三个 IMGS 重叠。

这是完整的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bugbox_wall_port" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
        <Button
            android:id="@+id/writeNewMsg"
            android:layout_margin="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New" />
        <RelativeLayout
            android:layout_width="match_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/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>
</ScrollView>

不知道怎么解决。快把我逼疯了。这种布局有什么问题?为什么元素没有排序?我该如何解决它并像往常一样对元素进行排序?

有人可以帮我吗?

提前谢谢你!

【问题讨论】:

  • 那么您的问题到底是什么?我不完全明白。
  • @0gravity 编辑了更多信息!
  • 3张图片一定要水平居中吗?
  • @0gravity 是的,它们必须水平居中。
  • 这是一张图片,它将以图形方式解释我的问题:imageshack.us/photo/my-images/198/sinttulo2kv.jpg

标签: android xml android-layout android-relativelayout


【解决方案1】:

小想法:

使用 android:src 代替 android:background 怎么样?

【讨论】:

    【解决方案2】:

    也许这就是你要找的东西:

    <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <Button
                android:id="@+id/writeNewMsg"
                android:layout_margin="10dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="New" />
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                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/icon"/>
                <ImageButton
                    android:id="@+id/playerMsgs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"                
                    android:background="@drawable/icon"
                    android:layout_toRightOf="@id/spyMsgs" />
                <ImageButton
                    android:id="@+id/allyMsgs"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/icon"
                    android:layout_toRightOf="@id/playerMsgs" />
             </RelativeLayout>
        </LinearLayout>
    </ScrollView>
    

    【讨论】:

    • 感谢您的工作,但它不起作用...在我的GraphicalLayout 中,我看到了您在此处发布的布局完全正确。但是在模拟器上却是杂乱无章的重叠……
    • 我在手机上运行它,它看起来与我发布的图像相同,可能问题出在其他地方?另外这3张图片有多大?你想水平滚动吗?
    • 这是一张图片,它将以图形方式解释我的问题:imageshack.us/photo/my-images/198/sinttulo2kv.jpg
    • 你发布的布局是整个布局?我刚刚在模拟器上运行了我的代码,它可以工作。因此,您的布局上可能还有其他问题导致了问题。
    • 我删除了 LinearLayouts。这是完整的代码:pastebin.com/reYE5RNQ
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2016-11-16
    • 2019-10-01
    • 2011-01-27
    • 1970-01-01
    • 2012-03-24
    • 2010-11-02
    相关资源
    最近更新 更多