ConstraintLayout将宽度设置为全宽

时间:2018-05-22 07:18:21

标签: android android-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);

2 个答案:

答案 0 :(得分:0)

您应该使用match_constraint0dp

  

小部件维度限制   可以通过以3种不同的方式设置android:layout_width和android:layout_height属性来指定小部件的维度:

     
      
  • 使用特定维度(文字值,如123dp或a   维度参考)
  •   
  • 使用WRAP_CONTENT,它将要求窗口小部件计算自己的大小
  •   
  • 使用0dp,相当于“MATCH_CONSTRAINT”
  •   

参考:https://developer.android.com/reference/android/support/constraint/ConstraintLayout

答案 1 :(得分:0)

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

我没有将它附加到父版面。

相关问题