【问题标题】:Can't move any elements in Android Studio for RelativeLayout无法在 Android Studio 中为 RelativeLayout 移动任何元素
【发布时间】:2019-01-25 13:33:48
【问题描述】:

我正在尝试将一些按钮/文本视图或任何其他组件添加到我的相对布局中。除了左上角,我不能把它们放在任何地方。但是,如果我编辑 XML 代码,它可以正常工作。

Here is the screenshot of current status

我已经将布局更改为 relativeLayout,但仍然没有运气。 (Android Studio 3.3)

我的布局 xml 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
</RelativeLayout>

我希望这些按钮/文本视图相对于彼此移动,但它们只是卡在左上角。

【问题讨论】:

  • 尝试将RelativeLayout 更改为LinearLayout。否则,您必须使用 android:layout_alignParentRight="true" 或 textview 中的选项 android:layout_alignRight="@id/button"
  • 可能首先你会读到一些关于RelativeLayout的东西?

标签: android android-layout android-relativelayout


【解决方案1】:

视图编辑器顶部有一个磁性按钮,称为(开/关自动连接)。如果你改变它,你可以在相对布局中移动视图。

【讨论】:

  • 这里有很好的推理。就今天而言,这确实帮助我使用了最新的 Android Studio (v3.4)。
【解决方案2】:

只需启用AutoConnection to Parent 在设计模式中单击磁铁图标并选择“AutoConnection to Parent”

【讨论】:

    【解决方案3】:

    这是如何防止所有内容在左角混乱的基本代码。

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:id="@+id/activity_main"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        tools:context=".MainActivity">
     <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@+id/textView"
            android:text="Button" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </RelativeLayout>
    

    要了解有关在相对布局中定位视图的更多信息,请参阅:

    https://developer.android.com/guide/topics/ui/layout/relative

    【讨论】:

      【解决方案4】:

      U 没有渲染相对布局规则。你有 layout_alignParentRight layout_alignParentLeft 等。你应该使用它们。例如,如果您希望您的 TextView 在按钮下方,您应该编写如下代码:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
          android:id="@+id/activity_main"
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity">
       <Button
              android:id="@+id/button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
             android:text="Button" />
      
          <TextView
              android:id="@+id/textView"
              android:layout_width="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_height="wrap_content"
              android:layout_below="@+id/button"
              android:text="TextView" />
      </RelativeLayout>
      

      https://developer.android.com/guide/topics/ui/layout/relative

      【讨论】:

        【解决方案5】:

        如果您没有下载与您在用于预览 XML 布局的“API 版本预览”选项卡中使用的相同的“SDK 平台工具”,则可能会出现此问题

        转到 Android XML Visualizer Tool -> 使用设计视图 -> 预览选项卡的 API 版本

        之后,检查API版本并确保下载与此类似的SDK平台工具

        【讨论】:

          【解决方案6】:

          只需勾选所有的 layout_alignParentXXXX 属性,然后您就可以看到元素在父级相对布局中移动,并带有边距。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-02-16
            • 2020-01-04
            • 2022-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多