【问题标题】:RelativeLayout not working properly Android相对布局无法正常工作Android
【发布时间】:2012-10-29 17:12:01
【问题描述】:

我有下一个问题:我经常使用 RelativeLayout,就像 Android 文档告诉我的那样。但是,几天后,我不明白为什么,这不再起作用了:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/boton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/boton"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000" />

</RelativeLayout>

这是我的布局,很简单。因此,根据这一点,textView 应该在按钮下方,但是,它却出现在按钮上方!

.

有人解释一下会发生什么吗?

编辑:问题不在于我尝试在设备上运行它,而是当我在 ADT Plugin for Eclipse 的图形布局上看到它时出现问题......

【问题讨论】:

  • 你结束了你的RelativeLayout,你的代码对我有用。 &lt;/RelativeLayout&gt;
  • 你的代码对我来说也很好用。你是否像 Asok 所说的那样结束了相对布局标签
  • 工作正常关闭相关布局标签。
  • 请记住,如果您的 xml 文件有错误导致无法正确构建,那么它将显示上次正常工作的预览。
  • 问题是不在设备上运行它......问题出现在Eclipse上ADT插件的图形布局上。

标签: android android-layout


【解决方案1】:

我试试这段代码,它工作正常。

如果您的一方无法正常工作,请尝试清理构建您的项目,然后运行。

如果仍然有问题,那么试试这个

android:layout_below="@+id/boton"

删除上面一行

android:layout_below="@id/boton"

因为“+”创建了对 R.java 文件的新引用

这可能是问题所在。

【讨论】:

    【解决方案2】:

    尝试从您的代码中删除以下行 android:layout_marginTop="60dp"

    【讨论】:

      【解决方案3】:

      从 TextView 的 layout_below 属性中去掉“+”号。 '+' 告诉它定义一个新的 id。如果没有“+”,它应该是指现有的。

      【讨论】:

      • 不管有没有+,它都可以工作,尽管+ 不应该在那里。
      【解决方案4】:

      我认为您的问题是背景的 drawable,因为我在您的预览中没有看到图像。

      遇到这个问题是给你一个build error,所以预览不会显示当前状态的布局(它会恢复到最后一次成功的构建)。删除该行并保存以查看预览或检查以确保您的 drawables 文件夹中有标题为“背景”的图像

      试试这个,不用drawable来检查它是否适合你:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      
          <Button
              android:id="@+id/boton"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:text="Button" />
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@+id/boton"
              android:layout_centerHorizontal="true"
              android:layout_marginTop="60dp"
              android:text="Large Text"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:textColor="#000" />
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案5】:

        我正在尝试运行它,现在,我知道出了什么问题。

        在终端中运行应用程序时,它工作正常,当我尝试在 Eclipse 上的 ADT 插件的图形布局上看到它时,问题就来了!有同样问题的人吗?

        【讨论】:

          【解决方案6】:

          在相对布局的末尾添加&lt;/RelativeLayout&gt; 以关闭它。

          【讨论】:

            猜你喜欢
            • 2014-06-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-17
            • 2012-10-10
            • 2018-04-02
            • 1970-01-01
            • 2017-08-03
            相关资源
            最近更新 更多