首页 > 解决方案 > 在我的设备上运行的应用程序与预览不同

问题描述

android java

button positioning

在我的设备上运行的显示与预览不同。宽度和高度不是固定的,这是在 ConstraintLayout 中。问题出在按钮上;我希望它看起来低于两个 TextViews。我应该用我的代码改变什么?(我确实尝试用谷歌搜索它,并寻找答案,但除了按钮的“absoluteX,absoluteY”代码之外找不到任何相关的东西)

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

 <Button
   android:id="@+id/button_button_view"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textAllCaps="true"
   android:text="order"
   tools:layout_editor_absoluteY="100dp"
   tools:layout_editor_absoluteX="4dp"/>

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAllCaps="true"
    android:text="quantity"
    android:textSize="16sp"
    app:layout_constraintLeft_toLeftOf="parent"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    app:layout_constraintBottom_toBottomOf="@id/quantity_text_view"
    app:layout_constraintTop_toTopOf="@id/button_button_view"/>

标签: javaandroidbuttonandroid-constraintlayoutpreview

解决方案


您必须使用属性来对齐视图

垂直对齐

layout_constraintTop_toBottomOf,layout_constraintBottom_toTopOf

水平对齐:

layout_constraintStart_toStartOf,layout_constraintEnd_toStartOf,layout_constraintStart_toEndOf

更新的 xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_button_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="order"
        android:textAllCaps="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text2" />

    <TextView
        android:id="@+id/quantity_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="quantity"
        android:textAllCaps="true"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/quantity_text_view" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读