首页 > 解决方案 > Android ConstraintLayout-2.0.0-alpha4 Fragment 与父级不匹配

问题描述

my 的布局FragmentConstraintLayout宽度和高度都设置为match_parent.

里面是另一个ConstraintLayout用来制作固定尺寸比例的区域。

它在 中完美运行ConstraintLayout-2.0.0-alpha3,但是在我更改为 alpha4 后,外部 ConstrainLayout 与父级不匹配。大约 70% 的原始大小对齐左上角,我不知道为什么。

片段附在 a 上FrameLayout,大小FrameLayout合适。

这是我的布局。

<?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=".KeyboardFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/game_keyboard_bg"
        app:layout_constraintDimensionRatio="1.6129"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">

        <Space
            android:id="@+id/corner_tl"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHeight_percent="0.01"/>

        <Space
            android:id="@+id/corner_br"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHeight_percent="0.01"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="2:1"
            app:layout_constraintTop_toBottomOf="@+id/corner_tl"
            app:layout_constraintBottom_toTopOf="@+id/corner_br"
            app:layout_constraintStart_toEndOf="@+id/corner_tl"
            app:layout_constraintEnd_toStartOf="@+id/corner_br" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-layout

解决方案


尝试这个

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout> 

推荐阅读