有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java约束布局在不同的设备上执行不同的操作

我正试图将我的相对布局转换为约束布局,以适合每台设备,但我遇到了一些问题。在Android Studio的编辑器上,它如下所示: enter image description here

我用一个象素C做模拟器。现在,我在三星Galaxy标签上试用了该应用程序,结果如下:enter image description here

为什么按钮不像第一张图片那样位于屏幕中央?这是xml的代码,您可以看到,我甚至锚定了元素以保持它们之间的距离:

<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/relativeLayout"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:background="@drawable/background_main"
安卓:scaleType="centerCrop"
tools:context=".MainActivity">

<TextView
    安卓:id="@+id/textView2"
    安卓:layout_width="708dp"
    安卓:layout_height="231dp"
    安卓:layout_marginTop="152dp"
    安卓:layout_marginEnd="8dp"
    安卓:layout_marginBottom="8dp"
    安卓:fontFamily="@font/kotta_one"
    安卓:text="Test del pensiero\n       divergente"
    安卓:textColor="#000000"
    安卓:textSize="86dp"
    安卓:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.57"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.195" />

<Button
    安卓:id="@+id/button_1"
    安卓:layout_width="180dp"
    安卓:layout_height="60dp"
    安卓:layout_marginStart="8dp"
    安卓:layout_marginEnd="76dp"
    安卓:layout_marginBottom="8dp"
    安卓:background="@drawable/button_bg"
    安卓:text="@string/invioA"
    安卓:textAllCaps="false"
    安卓:textColor="#FFFFFF"
    安卓:textSize="18dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button_2"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="@+id/textView2"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    app:layout_constraintVertical_bias="0.028" />

<Button
    安卓:id="@+id/button_2"
    安卓:layout_width="180dp"
    安卓:layout_height="60dp"
    安卓:layout_marginEnd="188dp"
    安卓:layout_marginBottom="8dp"
    安卓:background="@drawable/button_bg"
    安卓:text="@string/invioB"
    安卓:textAllCaps="false"
    安卓:textColor="#FFFFFF"
    安卓:textSize="18dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/textView2"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    app:layout_constraintVertical_bias="0.028" />

<Button
    安卓:id="@+id/button_3"
    安卓:layout_width="161dp"
    安卓:layout_height="114dp"
    安卓:layout_marginEnd="8dp"
    安卓:background="@安卓:color/transparent"
    安卓:fontFamily="@font/kotta_one"
    安卓:text="Area\nDocenti"
    安卓:textAllCaps="false"
    安卓:textColor="#030000"
    安卓:textSize="26dp"
    app:layout_constraintEnd_toStartOf="@+id/textView2"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</安卓.support.constraint.ConstraintLayout>

共 (1) 个答案

  1. # 1 楼答案

    You are giving wrong width and height to button and textview.

    Also, for horizontal button and same width and height you can use chain like below I used.

    Here, I give you approx design and also you can use your ConstraintLayout because I have androidx migrated project.

    Blockquote

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/add_news_feed_bg"
        android:scaleType="centerCrop">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:text="Test del pensiero\n       divergente"
            android:textColor="#000000"
            android:textSize="86dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/bg_toolbar"
            android:text="invioA"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            app:layout_constraintBottom_toBottomOf="@+id/button_2"
            app:layout_constraintEnd_toStartOf="@+id/button_2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/button_2" />
    
        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:background="@color/bg_toolbar"
            android:text="invioB"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="18dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/button_1"
            app:layout_constraintTop_toBottomOf="@+id/textView2" />
    
        <Button
            android:id="@+id/button_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:background="@android:color/transparent"
            android:text="Area\nDocenti"
            android:textAllCaps="false"
            android:textColor="#030000"
            android:textSize="26dp"
            app:layout_constraintBottom_toTopOf="@+id/textView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>