0

在我的 Android 应用程序中,我有一个活动,其中包含以下组件,包含在ConstraintLayout(all width=match_parent, height=wrap_content) 中:

title
scrollview
  linearLayout
    fragment1
    fragment2
    fragment3
    fragment4
adUnit

我希望 title 和 adUnit 保持固定,其余部分垂直滚动。我或多或少地发生了这种情况,但片段不再显示它们的所有内容——我认为它们都被设置为相同的高度。我如何在这里实现我想要的?

这是我当前的布局(精简到相关部分):

<android.support.constraint.ConstraintLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.v4.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        app:layout_constraintTop_toBottomOf="@id/tv_title">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <fragment
                android:id="@+id/fragment_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                tools:layout="@layout/fragment_1" />

            <fragment
                android:id="@+id/fragment_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/fragment_1"
                tools:layout="@layout/fragment_2" />

            <fragment
                android:id="@+id/fragment_3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/fragment_2"
                tools:layout="@layout/fragment_3" />

            <fragment
                android:id="@+id/fragment_4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/fragment_3"
                tools:layout="@layout/fragment_4" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

    <include
        android:id="@+id/adView"
        layout="@layout/layout_ad_view"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>
4

1 回答 1

1

您必须创建这种类型的布局。检查下面的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:title="Title" />


    <android.support.v4.widget.NestedScrollView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@id/adView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar">

        <FrameLayout
            android:id="@+id/frame_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </android.support.v4.widget.NestedScrollView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize=""
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

在上面的布局中,标题工具栏和 adView 固定在顶部和底部。在FrameLayout的帮助下替换您的片段,并且您还可以通过 java 类获得片段。为此检查示例 java 代码。

FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.frame_container, new Fragment1()); //Fragment2(),Fragment3(),Fragment4() 
        ft.commit();
于 2019-07-12T05:06:24.973 回答