【问题标题】:ConstraintLayout issue on android 6android 6上的ConstraintLayout问题
【发布时间】:2017-04-06 08:59:40
【问题描述】:

我在我的项目中使用 ConstraintLayout 发现了一个非常奇怪的错误。事实上,这个问题只存在于 android 6。

我正在使用以下 XML 来显示用于不同登录可能性的按钮列表:

<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:fitsSystemWindows="true"
tools:context="com.fagets.rainbowsixapp.login.LoginStepOneFragment">

<LinearLayout
    android:id="@+id/linearLayout_login_form"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintBottom_creator="1"
    android:layout_marginStart="65dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="65dp"
    app:layout_constraintRight_toRightOf="parent"
    tools:layout_constraintLeft_creator="1"
    android:layout_marginBottom="18dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="65dp"
    android:layout_marginRight="65dp">

    <com.facebook.login.widget.LoginButton
        xmlns:facebook="http://schemas.android.com/apk/res-auto"
        android:id="@+id/login_button_facebook"
        style="@style/LoginButton"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        facebook:com_facebook_login_text="@string/login_connect_facebook"/>

    <Button
        android:id="@+id/login_button_google"
        style="@style/LoginButton"
        android:textColor="@color/login_button_google_text"
        android:background="@drawable/login_button_shape_google"
        android:text="@string/login_connect_google"/>

    <Button
        android:id="@+id/login_button_anonymous"
        style="@style/LoginButton"
        android:background="@drawable/login_button_shape_anonymous"
        android:text="@string/login_connect_anonymous"/>

    <Button
        android:id="@+id/login_button_about"
        style="@style/LoginButton"
        android:background="@android:color/transparent"
        android:text="@string/login_connect_about"
        android:textColor="@color/login_button_google_text"/>

</LinearLayout>

我的问题与 LinearLayout 中的 app:layout_constraintBottom_toBottomOf="parent" 有关。在 Android 6 上,LinearLayout 被裁剪而不是粘在底部。它在 android 7 甚至 android 4.4.2 上都能正常工作。在那之后,我尝试删除我的 LinearLayout 以仅使用 ConstraintLayout,但我仍然遇到同样的问题。

这是 Android 7 的屏幕截图:

这里是 Android 6 的问题:

我做错了吗?还是 ConstraintLayout 库的问题?

谢谢!

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    当您使用约束布局时,您不需要额外的 LinearLayout

    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">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            android:layout_marginBottom="16dp"
            app:layout_constraintBottom_toTopOf="@+id/button1" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintHorizontal_bias="1.0"
            android:layout_marginBottom="16dp"
            app:layout_constraintBottom_toTopOf="@+id/button2" />
    
    </android.support.constraint.ConstraintLayout> 
    

    【讨论】:

    • 如果您阅读了我的所有出版物,您会发现我已经尝试过该解决方案。我仍然有同样的问题。
    • 好吧..我已经在 Andorid-6 设备上编译了这个 xml,它对我有用。我不认为这是 ConstraintLayout 的问题......但是这是我的 ConstraintLayout 版本...... com.android.support.constraint:constraint-layout:1.0.2
    • 我使用与您相同的版本,用于 ConstraintLayout。此外,我复制并粘贴了您的代码,即使在运行后问题与 Android 6 相同(使用 Nexus 5 和索尼 Xperia Z3 compact 进行测试)
    • 这很奇怪...我在 Nexus 5 上运行过...如果我有任何相关信息,我会通知您。
    • 这里有什么更新吗?我对 Android 6.0.1 上的约束布局也有问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2021-10-04
    • 2021-06-29
    相关资源
    最近更新 更多