java - Android Studio View 的显示方式与我的手机(三星 Galaxy S5)上的显示方式不同

标签 java android android-constraintlayout

我正在 Android 上创建一个应用程序并遇到问题。当我在 Android Studio 中进行编辑时,我的应用程序的所有元素看起来都符合预期。我正在使用 ConstraintLayout 并确保所有内容都使用 sp 作为其单位,以确保缩放发生。我尝试过更改边距和填充,但无论我做了什么,手机上的按钮都显示为被粉碎在一起。

以下是 Android Studio 中的 View (我希望它的外观): Android Studio View

这是它在我的手机(三星 Galaxy S5)上的外观:My Phone View

在我的手机上,按钮彼此相连,但在我的 Android Studio 上,按钮之间有空白。如何修改我的代码来创建我想要的图像?

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=".Settings">

    <Button
        android:id="@+id/homeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="homeMenu"
        android:text="@string/homeButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/calculateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginBottom="8dp"
        android:onClick="programMenu"
        android:text="@string/calculateButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/settingsIntroTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/settingsIntroTextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

最佳答案

尝试这个答案

<?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/homeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="homeMenu"
        android:text="return home"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
        android:layout_marginBottom="8dp" app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.964" android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/calculateButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="programMenu"
        android:text="Create program"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="8dp"
        app:layout_constraintTop_toTopOf="parent" android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toStartOf="@+id/homeButton" android:layout_marginStart="8dp"
        app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintVertical_bias="0.964"/>

    <TextView
        android:id="@+id/settingsIntroTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="setting"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

enter image description here

关于java - Android Studio View 的显示方式与我的手机(三星 Galaxy S5)上的显示方式不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55604025/

相关文章:

java - 我想将图像大小减小到 64 KiB 以保存在 mySql 数据库中

java.lang.NumberFormatException : Invalid long: ""

android - 子约束布局的约束布局不水平扩展

android - 在键盘打开的情况下约束布局滚动到底部

java - 我收到错误隐式 super 构造函数 SQLiteOpenHelper()

java - Android 播放外部 MP3 : error -- MediaPlayer start called in state 1

java - 即使servlet 数据库中不存在表employee,rs 也始终为true

android - MotionLayout - 日志中恼人的调试错误 : "no widget for <view id> (<view type>)"

java - 在android中通过fb登录后无法获取emailId

java - 'public static void' 在 Java 中是什么意思?