Android Studio,不同的预览和模拟器

Android Studio,不同的预览和模拟器,android,Android,第一张图片是预览的外观;但是,当我运行模拟器时,它看起来像第二张图片。我不知道为什么会这样。顺便说一下,我正在制作一个简单的drumbox应用程序 预演 仿真器 activity_main.xml的代码为 <Button android:id="@+id/bass" android:layout_width="150dp" android:layout_height="250dp" android:text="1" tools:layout_editor_absoluteX="25dp

第一张图片是预览的外观;但是,当我运行模拟器时,它看起来像第二张图片。我不知道为什么会这样。顺便说一下,我正在制作一个简单的drumbox应用程序

预演

仿真器

activity_main.xml的代码为

<Button
android:id="@+id/bass"
android:layout_width="150dp"
android:layout_height="250dp"
android:text="1"
tools:layout_editor_absoluteX="25dp"
tools:layout_editor_absoluteY="10dp" />

<Button
android:id="@+id/snare"
android:layout_width="150dp"
android:layout_height="250dp"
android:text="2"
tools:layout_editor_absoluteX="200dp"
tools:layout_editor_absoluteY="10dp" />

<Button
android:id="@+id/closedhihat"
android:layout_width="150dp"
android:layout_height="250dp"
android:text="3"
tools:layout_editor_absoluteX="25dp"
tools:layout_editor_absoluteY="250dp" />

<Button
android:id="@+id/hitom"
android:layout_width="150dp"
android:layout_height="250dp"
android:text="4"
tools:layout_editor_absoluteX="200dp"
tools:layout_editor_absoluteY="250dp" />

</android.support.constraint.ConstraintLayout>


有人能帮忙吗?

请检查我对这个问题的回答:

主要工具:layout_editor_absoluteX=“200dp”仅指编辑器,在约束条件下,布局位置应与布局中的其他元素或父元素有关。看看我的例子


希望对您有所帮助

您的问题在这段代码中

tools:layout_editor_absoluteX="200dp"
tools:layout_editor_absoluteY="250dp"
工具可以告诉Android Studio哪些属性在运行时被忽略,并且只有在设计布局时才有效

例如,我们希望android:text属性只在布局预览中工作,因此您可以这样做
tools:text=“我是一个文本”

就像代码中的
tools:layout\u editor\u absoluteY=“250dp”

在你的代码中试试这个

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

<Button
    android:id="@+id/bass"
    android:layout_width="150dp"
    android:layout_height="250dp"
    android:text="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/snare"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/snare"
    android:layout_width="150dp"
    android:layout_height="250dp"
    android:text="2"
    app:layout_constraintLeft_toRightOf="@id/bass"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/closedhihat"
    android:layout_width="150dp"
    android:layout_height="250dp"
    android:text="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/hitom" />

<Button
    android:id="@+id/hitom"
    android:layout_width="150dp"
    android:layout_height="250dp"
    android:text="4"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toRightOf="@id/closedhihat"
    app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

注意

ConstraintLayout属性

  • 布局_constraintLeft _toLeftOf
  • 布局_constraintLeft _toRightOf
  • 布局图_constraintRight _toLeftOf
  • 布局图
  • 布局_constraintop _toTopOf
  • 布局图_约束图_toBottomOf
  • 布局_约束底部_toTopOf
  • 布局\u约束底部\u toBottomOf
  • 布局_约束基线_tobalineof
  • 布局\u约束开始\u结束
  • 布局\u约束开始\u到开始
  • 布局\u约束倾向\u开始
  • 布局_约束_toEndOf
可能的副本