首页 > 解决方案 > ConstraintLayout 将宽度设置为全宽

问题描述

我有主要约束布局,还有额外的布局页脚,我将其膨胀并将其添加到我的主要约束中。问题是,即使我设置约束以将宽度设置为完整(match_parent),它也不能将视图居中,但它会包装内容,但它没有扩展到整个屏幕。这是我的代码:

页脚:

<?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"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="@color/background">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

主布局基本上是空约束布局,比我设置的代码如下:

View footerRow = layoutInflater.inflate(R.layout.footer, null, false);
footerRow.setId(R.id.constraint_footer);
mainView.addView(footerRow);

ConstraintSet set = new ConstraintSet();
set.clone(mainView);
set.connect(footerRow.getId(), ConstraintSet.LEFT, ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
set.connect(footerRow.getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);
set.connect(footerRow.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
set.applyTo(mainView);

标签: androidandroid-constraintlayout

解决方案


基本上这个问题解决了我的问题:Nested Constraint Layout not show if using Match_Constraints

我没有将它附加到父布局。


推荐阅读