簡體   English   中英

嵌套協調器布局和應用欄布局

[英]Nested Coordinator layout and Appbar Layout

嘗試構建活動具有協調器布局和應用欄布局的布局。 appbar布局具有選項卡布局,該選項卡布局連接到視圖分頁器。 在視圖分頁器中,第一個片段具有另一個協調器布局和一個appbar布局。 當滾動在應用程序欄布局中發生時,我希望活動的應用程序欄目也進行滾動。

活動的xml

    <?xml version="1.0" encoding="utf-8"?>


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

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapse_layout"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                app:layout_scrollFlags="scroll|snap|enterAlwaysCollapsed">


                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:src="@drawable/material_flat"
                    app:layout_collapseMode="parallax" />


                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    style="@style/ToolBarWithNavigationBack"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@android:color/transparent"
                    app:layout_collapseMode="pin"></android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>

            <LinearLayout
                android:id="@+id/tab_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@android:color/white"
                android:clipToPadding="true"
                android:gravity="center"
                android:orientation="vertical"
                android:paddingTop="10dp"
                app:layout_scrollFlags="scroll|enterAlways|snap">

                <android.support.design.widget.TabLayout
                    android:layout_width="fill_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="bottom"
                    app:tabIndicatorColor="#BDBDBD"
                    app:tabIndicatorHeight="4dp"
                    app:tabSelectedTextColor="?android:attr/textColorPrimaryInverse"
                    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" />
            </LinearLayout>

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

        <android.support.v4.view.ViewPager
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

片段的xml

            <?xml version="1.0" encoding="utf-8"?>
             <NestedCoordinatorLayout 
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:background="@android:color/background_light">

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    app:theme="@style/ThemeOverlay.AppCompat.Dark">


    <LinearLayout
        android:id="@+id/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e3e6e8"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap">

        <android.support.v7.widget.CardView
            card_view:cardCornerRadius="4dp"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_margin="24dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/shade_gradient" />

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

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/simple.viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

     </NestedCoordinatorLayout>

當應用程序欄在片段中滾動時,我希望活動的應用程序欄也滾動。 我已經在另一個CoordinatorLayout中的CoordinatorLayout中使用了NestedCoordinatorLayout,請讓我知道如何實現該功能,謝謝!

嘗試在相對布局中嵌套嵌套ScrollView,如下所示

         <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/tools"
 android:background="@android:color/background_light">

<NestedCoordinatorLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    app:theme="@style/ThemeOverlay.AppCompat.Dark">


    <LinearLayout
        android:id="@+id/home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#e3e6e8"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="20dp"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap">

        <android.support.v7.widget.CardView
            card_view:cardCornerRadius="4dp"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_margin="24dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/shade_gradient" />

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

    </LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/simple.viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

     </NestedCoordinatorLayout>

   </RelativeLayout>

暫無
暫無

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

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