首页 > 解决方案 > 我的拖放在 Android Studio 中不起作用

问题描述

正如您在上图中看到的那样,当您拖动某些东西时,有“to RightOf=button”、“below=button”的对齐选项,但是当我拖动某些东西时,没有显示任何类似的东西。谁能帮我解决这个问题?

图片

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">


    <TextView
        android:id="@+id/text_witaj_uzytkowniku"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/witaj_uzytkowniku" />

</RelativeLayout>

标签: javaandroidandroid-layoutmobile

解决方案


老实说,我更喜欢使用 ConstraintLayout 作为根。我相信使用拖放工具会更容易。

使用它时,请确保所有对象彼此(或父对象)至少垂直连接一次,水平连接一次。这在代码中的外观:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="24dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="@+id/textView9"
        app:layout_constraintTop_toBottomOf="@+id/textView9" />
</androidx.constraintlayout.widget.ConstraintLayout>

您还可以使用侧面板调整距离。

要使用布局,您只需在 Pallete 中键入 Constraint layout 并拖动它(如果未安装,将有下载)


推荐阅读