簡體   English   中英

如何創建一個內部帶有ViewPager的折疊工具欄?

[英]How to create a collapsing toolbar with a ViewPager inside?

我有一個帶有ViewPager (用於圖像)的ScrollView和一個用於點指示器的LinearLayout 我還想要一個可折疊的工具欄,其中ViewPagerLinearLayout 我已經添加了我的代碼以及可折疊工具欄的代碼。 但是我不知道如何在其中實現ViewPagerLinearLayout 請有人給我正確的工作代碼?

我的代碼

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/aboutslscrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

        <LinearLayout
            android:id="@+id/SliderDots"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/viewPager"
            android:layout_marginBottom="20dp"
            android:gravity="center_horizontal"
            android:orientation="horizontal" />


    </RelativeLayout>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#FFFFFF">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="About"
        android:textColor="@color/colorPrimary"
        android:paddingHorizontal="10dp"
        android:textSize="20sp" />
    </LinearLayout>
    </LinearLayout>
    </ScrollView>

為可折疊工具欄找到的代碼

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary">

        <ImageView
            android:id="@+id/expandedImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@drawable/beach_scene"

            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"
         app:layout_collapseParallaxMultiplier="0.7"
            />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" >


        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/sample_string"/>

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

</android.support.design.widget.CoordinatorLayout>

您能告訴我在可折疊工具欄中添加ViewPager和dotindicator( LinearLayout )的位置嗎?

ViewPager和LinearLayout的容器布局-RelativeLayout應該在CollapsingToolbarLayout內部。 並像這樣設置屬性,使其可與滾動事件折疊。

app:layout_collapseMode="parallax"

解:

<android.support.design.widget.CoordinatorLayout                 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.birjuvachhani.myapplication.ScrollingActivity">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/app_bar_height"
            android:fitsSystemWindows="true"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:titleEnabled="false"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:toolbarId="@+id/toolbar">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_collapseMode="parallax">

                        <android.support.v4.view.ViewPager
                            android:id="@+id/viewPager"
                            android:layout_width="match_parent"
                            android:layout_height="250dp" />

                        <LinearLayout
                            android:id="@+id/SliderDots"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignBottom="@id/viewPager"
                            android:layout_marginBottom="20dp"
                            android:gravity="center_horizontal"
                            android:orientation="horizontal" />

                </RelativeLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >

        <!-- Main content goes here. -->

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM