如何实现以下布局?

时间:2019-07-11 16:54:30

标签: android-layout android-constraintlayout android-scrollview

在我的Android应用中,我有一个活动,其中包含包裹在ConstraintLayout(所有width=match_parentheight=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>

1 个答案:

答案 0 :(得分: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();
相关问题