【问题标题】:All widgets in Constraint Layout getting stuck to upper left corner in Android Studio约束布局中的所有小部件都卡在 Android Studio 的左上角
【发布时间】:2020-11-25 05:47:15
【问题描述】:

我最近开始在 android studio 中编码。两周前我在约束布局中进行了这个设计,今天我重新打开了这个项目以做更多的工作,但现在所有的小部件都卡在了屏幕的左上角。我已经为所有小部件分配了约束。 我搜索了这个问题,并尝试使用“自动连接到父级”按钮和“推断”按钮(根据我的理解,您选择一个小部件,然后单击推断和自动连接相同)但似乎没有任何工作。

我在屏幕截图中选择了一个 ID 为 textView10 的小部件。

任何帮助将不胜感激,谢谢!

设计模板:

XML 代码 sn-p:

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    tools:context=".StudentActivity">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="49dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="60dp"
        android:text="Student Details"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        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_marginStart="37dp"
        android:layout_marginLeft="37dp"
        android:layout_marginTop="57dp"
        android:layout_marginEnd="27dp"
        android:layout_marginRight="27dp"
        android:text="Name"
        android:textSize="25dp"
        app:layout_constraintEnd_toStartOf="@+id/editText8"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView9" />

【问题讨论】:

  • 只需将您的 android.support.constraint.ConstraintLayout 更改为 androidx.constraintlayout.widget.ConstraintLayout。如果您不使用 androidx,请将您的项目更改为 androidx

标签: android-studio android-layout android-constraintlayout


【解决方案1】:

尝试更改 xml 顶部的标记,即:

android.support.constraint.ConstraintLayout

到这里:

androidx.constraintlayout.widget.ConstraintLayout

应该可以!

【讨论】:

  • 不能直接工作,就像@Syed Rafaqat Hussain 说的,我想我得把项目改成androidx
  • 哦对了,我忘了说你必须改变顶部的底部的标签。和我猜的接受的答案一样
  • 是的,是一样的
【解决方案2】:

尝试以下代码片段

<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"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="60dp"
        android:layout_marginLeft="60dp"
        android:layout_marginTop="49dp"
        android:layout_marginEnd="60dp"
        android:layout_marginRight="60dp"
        android:text="Student Details"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="40dp"
        app:layout_constraintEnd_toEndOf="parent"
        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_marginStart="37dp"
        android:layout_marginLeft="37dp"
        android:layout_marginTop="57dp"
        android:layout_marginEnd="27dp"
        android:layout_marginRight="27dp"
        android:text="Name"
        android:textSize="25dp" 
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView9" />

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-29
    • 2018-11-04
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    相关资源
    最近更新 更多