Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在Activity中使用ActionBar的片段中使用CoordinatorLayout大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_618_0@
我在使用 Android设计支持库时遇到问题.我想在滚动时使用内部片段中的CoordinatorLayout进行FAB和ActionBar动画,但我在活动布局中使用ActionBar,在片段中使用CardGridStaggeredView和FAB.

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://scheR_661_11845@as.android.com/apk/res/android"
    xmlns:app="http://scheR_661_11845@as.android.com/tools"
    xmlns:app1="http://scheR_661_11845@as.android.com/apk/res-auto"
    xmlns:tools="http://scheR_661_11845@as.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

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

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

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:BACkground="?attr/colorPriMary"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:layout_collapseMode="pin"
                    app:layout_scrollFlags="scroll|enterAlways" />

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

            <FrameLayout   <!--Here I display fragments-->
                android:id="@+id/fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        app1:itemTextColor="@android:color/black"/>

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

片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://scheR_661_11845@as.android.com/apk/res/android"
                xmlns:tools="http://scheR_661_11845@as.android.com/tools"
                xmlns:card="http://scheR_661_11845@as.android.com/apk/res-auto"
                xmlns:app="http://scheR_661_11845@as.android.com/apk/res-auto"
                style="@style/FragmentTheme"
                tools:context="androidhive.info.materialdesign.activity.HomeFragment">

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

        <android.support.v4.widget.Swipe@R_675_7017@hLayout
            android:id="@+id/swipe_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <it.gmariotti.cardslib.library.extra.staggeredgrid.view.CardGridStaggeredView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numcolumns="2"
                android:stretchMode="columnWidth"
                android:verticalSpacing="2dp"
                android:horizontalSpacing="2dp"
                android:padding="1dp"
                android:gravity="center"
                card:list_card_layout_resourcEID="@layout/carddemo_extras_base_staggered_card"
                style="@style/main_layout_foreground"
                android:id="@+id/cgvImages"
                app:layouT_Behavior="@String/appbar_scrolling_view_behavior"/>

        </android.support.v4.widget.Swipe@R_675_7017@hLayout>

        <android.support.design.widget.FloaTingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add"
            android:layout_gravity="bottom|end"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp"/>

    </FrameLayout>
</RelativeLayout>

我试图在Activity和Fragment中使用COordinatorLayout,但它没有用.有没有办法在Activity中使用COordinatorLayout和ActionBar以及片段中的其他视图?

解决方法

我有使用DrawerLayout的Activity和使用COordinatorLayout的片段工作得很好(注意在哪里设置了fitsSystemWindows属性)

这是活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://scheR_661_11845@as.android.com/apk/res/android"
    xmlns:app="http://scheR_661_11845@as.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:BACkground="@color/color_priMary" />

    <android.support.design.widget.NavigationView
        android:id="@+id/menu_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

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

            <!-- Listview to display slider menu -->
            <ListView
                android:id="@+id/list_slidermenu"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:BACkground="@color/bm_white"
                android:choiceMode="singleChoice"
                android:listSELEctor="@drawable/list_SELEctor"
                android:dividerHeight="0dp"
                android:divider="@null" />
            <!-- Картинки - fb,vk,twitter  -->

        </LinearLayout>

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

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

和片段的xml与协调器,操作按钮等:

<?xml version="1.0" encoding="utf-8"?>
<!--NOT SET HERE: android:fitsSystemWindows="true"-->
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://scheR_661_11845@as.android.com/apk/res/android"
    xmlns:app="http://scheR_661_11845@as.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- NOT SET HERE:  android:fitsSystemWindows="true" -->
    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/detail_BACkdrop_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPriMary"
            app:expandedtitleMarginEnd="64dp"
            app:expandedtitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

                <ImageView
                    android:id="@+id/BACkdrop"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentTop="true"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/pic_top_main" />

            </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:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

        </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:layout_gravity="fill_vertical"
        app:behavior_overlaPTOP="32dp"
        app:layouT_Behavior="@String/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/ll_main_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- In code Now… -->
            <!--<include layout="@layout/fragment_start_nonfc"/>-->
            <!--<include layout="@layout/fragment_start"/>-->

        </LinearLayout>

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

    <android.support.design.widget.FloaTingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:onClick="onClickPurchase_Remote"
        android:src="@drawable/btn_purchase"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:fabSize="normal"
        app:pressedTranslationZ="12dp" />

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

大佬总结

以上是大佬教程为你收集整理的android – 在Activity中使用ActionBar的片段中使用CoordinatorLayout全部内容,希望文章能够帮你解决android – 在Activity中使用ActionBar的片段中使用CoordinatorLayout所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。