我的拖放功能在 Android Studio 中不起作用

问题描述 投票:0回答:5

正如您在上图中看到的,当您拖动某些内容时,有对齐选项“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>
java android android-layout mobile
5个回答
1
投票

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

使用时请确保all objects are connected to each other(或父级)至少垂直一次和水平一次。 这在代码中看起来如何:

<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>

您还可以使用 side panel 调整距离。

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


0
投票

设置

textview
的位置,然后尝试通过拖动来添加另一个视图

<?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:layout_alignParentTop ="true"
        android:layout_alignParentLeft="true"
        android:text="@string/witaj_uzytkowniku" />


</RelativeLayout>

0
投票

我认为您应该使用 CoordinatorLayout 而不是relativelayout。 如果您继续使用相对布局,然后转到文件无效并重新启动您的 Android 工作室(如果这是与缓存相关的问题),然后解决它。


0
投票

使用协调器布局来工作。 relativelayout更多的是编码布局..


0
投票

转到设置>外观和行为>外观 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.