将约束设置为片段

时间:2019-07-16 19:35:25

标签: android android-constraintlayout

我正在尝试向我的活动中动态添加一个片段,该片段将在要放入其中的约束布局内具有某些约束。

简而言之,我想要的是将片段添加到约束布局中。约束布局的边界覆盖整个屏幕,但顶部有一个标题。我希望该片段具有带有标题的顶部-底部约束和带有父约束布局的底部-底部约束

我尝试将约束放入片段的布局XML中 我尝试用ConstraintSet以编程方式设置约束条件

活动布局XML(主要活动)

<android.support.constraint.ConstraintLayout
        android:id="@+id/main_activity_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.seven.eleven.ideation.views.HeaderLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="@dimen/header_height"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

活动代码

getSupportFragmentManager().beginTransaction()
                    .add(R.id.main_activity_layout, homeFragment, HOME_TAG).commit();

            ConstraintSet constraintSet = new ConstraintSet();
            constraintSet.clone(mainLayout);
            constraintSet.connect(R.id.fragment_home_root,  ConstraintSet.TOP,
                                    R.id.header,            ConstraintSet.BOTTOM);
            constraintSet.connect(R.id.fragment_home_root,  ConstraintSet.BOTTOM,
                                    R.id.root,              ConstraintSet.BOTTOM);
            constraintSet.applyTo(mainLayout);

片段布局XML(主片段)

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_home_root"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@+id/header"
    app:layout_constraintBottom_toBottomOf="parent">

    <TextView
        android:id="@+id/welcome_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/welcome_paragraph_text"
        android:layout_margin="@dimen/box_margins"
        android:text="@string/welcome"
        android:textSize="@dimen/oversized_text_size"
        android:textColor="@color/primary_text_color"
        android:textStyle="bold"/>

    <TextView
        android:id="@+id/welcome_paragraph_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginLeft="@dimen/box_margins"
        android:layout_marginRight="@dimen/box_margins"
        android:text="@string/welcome_paragraph"
        android:textSize="@dimen/primary_text_size"
        android:textColor="@color/primary_text_color"/>

</android.support.constraint.ConstraintLayout>

根据我使用的方法,片段要么位于顶部,要么位于整个屏幕的中央,而不是被限制在“页眉”的底部

1 个答案:

答案 0 :(得分:0)

如果有人遇到类似问题,也许是我的解决方法可以帮助您

不要将Fragment放置在父约束布局中,而是使用所需的约束创建新的FrameLayout并将Fragment放置在其中。

我为Main Activity使用相同的布局

<android.support.constraint.ConstraintLayout
        android:id="@+id/main_activity_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.seven.eleven.ideation.views.HeaderLayout
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="@dimen/header_height"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:id="@+id/fragment_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@+id/header"
            app:layout_constraintBottom_toBottomOf="parent"/>

    </android.support.constraint.ConstraintLayout>

并在活动代码中

getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_layout, homeFragment, HOME_TAG).commit();

您可以看到FrameLayout具有我想要的期望约束。无需以编程方式添加它们,也无需在其他布局XML中使用id进行假设。现在,FrameLayout将成为Fragment的父级