android - 为 fragment 设置约束

标签 android android-constraintlayout

我正在尝试向我的 Activity 动态添加一个 Fragment,该 Fragment 将在放置它的 Constraint Layout 中具有某些约束。

简而言之,我想要的是将 fragment 添加到约束布局。 Constraint Layout 的边界覆盖了整个屏幕,但在顶部有一个 Header。我希望 Fragment 具有带有标题的 Top-Bottom 约束和带有父 Constraint Layout 的 Bottom-Bottom 约束的约束

我试过将约束放在 fragment 的布局 XML 中 我尝试以编程方式使用 ConstraintSet 设置约束

Activity 布局 XML(主 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" />

    </android.support.constraint.ConstraintLayout>

Activity 代码

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);

fragment 布局 XML(主页 fragment )

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

根据我使用的方法,Fragment 要么到达顶部,要么位于整个屏幕的中心,而不是被限制在 Header 的底部

最佳答案

如果遇到这个问题的人有类似的问题,也许我的解决方法可以提供帮助:

不要将 Fragment 放置在父 Constraint Layout 中,而是使用您想要的约束创建一个新的 FrameLayout 并将 Fragment 放置在其中。

我在主 Activity 中使用相同的布局

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

        <com.my.project.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>

在 Activity 的代码中

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

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

关于android - 为 fragment 设置约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57064307/

相关文章:

android - 约束布局 : Compound ImageView that stays in parent when text expands

android - 将动态 subview 垂直添加到 ConstraintLayout

android - 在没有 ActionBarSherlock 的情况下在 Actionbar ShareActionProvider 上设置自定义共享图标

java - 关闭整个应用程序android的声音

android - 约束布局中相对布局的layout_above?

android - 约束布局纵横比

android - 如何将 ButtonView 定位到具有大单行文本的 TextView 的末尾而不将其移出屏幕

android - 第一次后未显示 Viewpager fragment

android - Activity 生命周期观察者 - 如何知道 Activity 实例何时被销毁?

java - 如何将其他 xml 文件链接到 android 中的 main_activity.xml 并访问它?