gpt4 book ai didi

android - 为 fragment 设置约束

转载 作者:行者123 更新时间:2023-11-29 23:00:02 27 4
gpt4 key购买 nike

我正在尝试向我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com