java Android Studio 的约束布局无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42768266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:51:50  来源:igfitidea点击:

Constraints Layout of Android Studio doesn't work correctly

javaandroidandroid-layoutandroid-studiomobile

提问by Son_Nguyen

I'm new in Android, i just create my first simple app on android studio, but constraints layout doesn't work correctly on real phone or virtual phone. I intent to put a button on the center of layout, and an edit text at top of layout, but when i run, both of it located same position at right-top layout, so i can't type a text and press the button.
I try to use RelativeLayout and it worked, so i guess the problem come from ConstraintLayout. Please help me fix it!

I have already install"ConstraintsLayout For Android 1.0.1 or lower", and "Solver For ConstraintsLayout 1.0.1 or lower". In build.gradle have added compile 'com.android.support.constraint:constraint-layout:1.0.1' I use Android Studio version 2.3. And here is my xml code:

我是 Android 新手,我只是在 android studio 上创建了我的第一个简单应用程序,但约束布局在真实手机或虚拟手机上无法正常工作。我打算在布局的中心放一个按钮,在布局的顶部放一个编辑文本,但是当我运行时,它们都位于右上角布局的相同位置,所以我无法输入文本并按下按钮.
我尝试使用RelativeLayout并且它起作用了,所以我猜问题来自ConstraintLayout。请帮我修复它!

我已经安装了“ConstraintsLayout For Android 1.0.1 或更低版本”和“Solver For ConstraintsLayout 1.0.1 或更低版本”。在 build.gradle 中添加了 compile 'com.android.support.constraint:constraint-layout:1.0.1' 我使用 Android Studio 2.3 版。这是我的 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="my.android.myfirstapp.MainActivity">


<Button
    android:id="@+id/btn_jump"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Jump Activity"
    tools:layout_editor_absoluteX="128dp"
    tools:layout_editor_absoluteY="269dp" />

<EditText
    android:id="@+id/edt_Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text=""
    tools:layout_editor_absoluteX="85dp"
    tools:layout_editor_absoluteY="62dp" />
</android.support.constraint.ConstraintLayout>

And here is my simple app when run:
enter image description here

这是我运行时的简单应用程序:
在此处输入图片说明

采纳答案by chirag90

Your constraints weren't setup correct, If you are unsure click on the button shown in the image below to see what the issue is.. usually all the layout issues are shown there.

您的约束设置不正确,如果您不确定单击下图中显示的按钮以查看问题所在......通常所有布局问题都显示在那里。

Layout issue

布局问题

Use the below code it fixes the issue for constraint.

使用下面的代码可以解决约束问题。

<?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:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">


    <Button
        android:id="@+id/btn_jump"
        android:layout_width="128dp"
        android:layout_height="48dp"
        android:text="Jump Activity"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edt_Name"
        android:layout_width="215dp"
        android:layout_height="43dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text=""
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="107dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.502" />
</android.support.constraint.ConstraintLayout>

Hope this help, if it works do mark the answer

希望这会有所帮助,如果有效,请标记答案