微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 工具栏无法从折叠工具栏中显示

这是代码.
<?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/main"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|enteralways|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/mytoolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:title="@string/app_name"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Light"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/main_tablayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:layout_collapseMode="none"/>


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

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

    <!--<android.support.v4.widget.nestedScrollView-->
        <!--android:id="@+id/nestedscroll"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="match_parent"-->
        <!--android:fillViewport="true"-->
        <!--android:scrollbars="horizontal"-->
        <!--app:layout_behavior="@string/appbar_scrolling_view_behavior">-->

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/main_fab"
        android:layout_margin="16dp"
        android:src="@android:drawable/ic_media_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/main_viewpager"
        app:layout_anchorGravity="bottom|end"/>

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

问题:
1)工具栏不可见.
2)折叠工具栏根本不会崩溃. [解决了]
3)如果放在nestedScrollView中,Viewpager和FAB也不可见. [解决了]

额外细节:
Viewpager片段的布局将Linearlayout作为root,内部具有recyclerview.

根据代码,一切似乎都没问题.无法理解缺少的东西.协调器布局和折叠工具栏如何协同工作的一个很好的解释也确实有帮助.

解决方法

1) Toolbar is not visible.

首先,您需要定义要在活动类中使用的工具栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);

更改现有的xml代码

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            app:title="@string/app_name"
            app:layout_collapseMode="parallax">
        </android.support.v7.widget.Toolbar>

至:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"  //set initial height
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" //this might be also useful
            app:title="@string/app_name"
            app:layout_collapseMode="parallax" />

2) Collapsing toolbar doesn’t collapse at all.

您的活动使用了正确的主题吗?设置为AppBarLayout:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"

如下例所示:include_list_viewpager.xml

3) Viewpager and FAB also not visible if put inside nestedScrollView.

没有理由这样做.添加这些行:

android:layout_marginTop="?attr/actionBarSize" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"

到ViewPager就足够了.

他们都应该是CoordinatorLayout的直接子女.

请遵循以下示例:http://blog.nkdroidsolutions.com/collapsing-toolbar-with-tabs-android-example/

如果您是Material Design的新手,或者对某些行为感到有些失落,我强烈建议您查看Chris Banes Material Design项目cheesequare:https://github.com/chrisbanes/cheesesquare/

希望它会有所帮助

原文地址:https://www.jb51.cc/android/309859.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐