Android studio在模拟器上的不同布局

时间:2017-07-02 12:06:30

标签: android xml layout

我刚刚开始使用android studio,我插入了一个按钮和文本字段。在预览面板中看起来不错,但是当我运行应用程序时,它的布局就不同了。 这是xml代码:

<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"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"

tools:context="com.example.alexander.myapplication.MainActivity">


<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    tools:ignore="HardcodedText" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    tools:layout_editor_absoluteX="119dp"
    tools:layout_editor_absoluteY="222dp"
    tools:ignore="HardcodedText,MissingConstraints"
    android:onClick="buttonClicked"
    />

This is how it looks like 谢谢!

2 个答案:

答案 0 :(得分:0)

看看以下几行:

tools:layout_editor_absoluteX="119dp"
tools:layout_editor_absoluteY="222dp"

他们说的是你只是在编辑器预览中将按钮置于绝对位置但是。 当应用程序实际运行时,该按钮没有实际位置值(注意参数名称中的“editor”前缀)。

您需要在编辑器中定义实际值。

在Android开发者网站上查看此指南: Build a Responsive UI with ConstraintLayout

答案 1 :(得分:0)

我刚刚通过将布局设置为RelativeLayout来解决问题。

相关问题