无法在Android Studio中移动RelativeLayout中的任何元素

12

我想在我的RelativeLayout中添加一些按钮/文本视图或其他组件。但是我只能把它们放在左上角,而无法放置在其他位置。然而,如果我编辑XML代码,则可以正常工作。

这是当前状态的截图

我已经将布局更改为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" - Pierre
五月一日,你读了一些关于RelativeLayout的内容吗? - grabarz121
6个回答

25

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


这里的推理很棒。这确实帮助了我最新的Android Studio(v3.4),就今天而言。 - Joshua Kusaasira

19

只需启用 自动连接到父级 在设计模式下,单击磁铁图标并选择“自动连接到父级” 输入图片描述


0

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

前往Android XML可视化工具 -> 使用设计视图 -> API版本预览选项卡。

How to Navigate

接下来,检查API版本并确保下载类似的SDK平台工具


0

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


0
这是如何防止所有内容混乱在左上角的基本代码。
<?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


0

你没有渲染相对布局规则。你有 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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接