java 项目在 Android Studio 模拟器中的位置错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42724144/
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:50:03  来源:igfitidea点击:

Items are in wrong place in Android Studio emulator

javaandroidxmlandroid-layoutandroid-emulator

提问by A. Sharkh

I'm new to android studio and this is my first app. It works correctly but having an issue with the emulator, it shows the button in the wrong place.

我是 android studio 的新手,这是我的第一个应用程序。它工作正常,但模拟器出现问题,它在错误的位置显示按钮。

<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="com.example.dell_pc.myapplication.MainActivity">

    <EditText
        android:id="@+id/txtyear"
        android:layout_width="193dp"
        android:layout_height="60dp"
        android:ems="10"
        android:hint="Enter year of birth"
        android:inputType="number"
        tools:layout_editor_absoluteY="129dp"
        tools:layout_editor_absoluteX="96dp" />

    <Button
        android:id="@+id/butage"
        android:layout_width="115dp"
        android:layout_height="41dp"
        android:onClick="BuGetAge"
        android:text="Show Age"
        tools:layout_editor_absoluteY="277dp"
        tools:layout_editor_absoluteX="135dp" />

</android.support.constraint.ConstraintLayout>

How can I make it to appear in the right place?

我怎样才能让它出现在正确的地方?

picture of the emulator

模拟器的图片

采纳答案by Aimy_N_Chu

Do you absolutely need to use constraint layout? If not, you can use a relative layout, and use layout_centerHorizontal and layout_centerVertical like this:

你绝对需要使用约束布局吗?如果没有,您可以使用相对布局,并像这样使用 layout_centerHorizo​​ntal 和 layout_centerVertical:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <EditText
        android:id="@+id/edittext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="blah blah"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="press"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/edittext"
        />

</RelativeLayout>

回答by Parman Josan

Select Component/Item(such as BUTTON) in activity_main.xml -> click on "Infer Constrains(as I show in attached file)"

在activity_main.xml 中选择组件/项目(例如按钮)-> 单击“推断约束(如我在附件中所示)”

Your item will take right place in ASE

您的物品将在 ASE 中正确放置

Do same for the rest Component/Items

对其余组件/项目执行相同操作

Enjoy

享受

回答by Okas

tools:layout_editor_absoluteY and other attributes from tools namespace only have effect in layout editor. Your working code does not use these attributes, so your views are in upper left corner, because this is default. As noted above RelativeLayout or even LinearLayout suits better such simple layout.

tools:layout_editor_absoluteY 和来自工具命名空间的其他属性仅在布局编辑器中有效。您的工作代码不使用这些属性,因此您的视图位于左上角,因为这是默认设置。如上所述,RelativeLayout 甚至 LinearLayout 更适合这种简单的布局。

回答by hev1

Click on an element and click the Magic Wand icon in the toolbar above the design view to Infer Constraints so the element will not jump to the (0, 0) position at runtime.

单击一个元素,然后单击设计视图上方工具栏中的 Magic Wand 图标来推断约束,以便该元素在运行时不会跳到 (0, 0) 位置。

enter image description here

在此处输入图片说明

回答by Rzouga Younsi

Just change:

只是改变:

</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

to

</RelativeLayout>

</RelativeLayout>

Problem solved! Be happy :D

问题解决了!开心点 :D