android – 使用TabLayout添加片段时的Material Design高程问题

android – 使用TabLayout添加片段时的Material Design高程问题,第1张

概述我目前正在处理v22的应用程序并使用以下库: compile 'com.android.support:appcompat-v7:22.2.1'compile 'com.android.support:support-v4:22.2.1'compile 'com.android.support:design:22.2.1' 我只有一个名为HomeActivity的Activity,里面有一个N 我目前正在处理v22的应用程序并使用以下库:

compile 'com.androID.support:appcompat-v7:22.2.1'compile 'com.androID.support:support-v4:22.2.1'compile 'com.androID.support:design:22.2.1'

我只有一个名为HomeActivity的Activity,里面有一个Navigation Drawer.@H_419_14@这是activity_home.xml布局:

<androID.support.v4.Widget.DrawerLayoutxmlns: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:ID="@+ID/drawer"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:fitsSystemwindows="true"tools:openDrawer="start"><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:fitsSystemwindows="false">    <androID.support.design.Widget.AppbarLayout        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:theme="@style/themeOverlay.AppCompat.Dark.Actionbar">        <androID.support.v7.Widget.Toolbar            androID:ID="@+ID/toolbar"            androID:layout_wIDth="match_parent"            androID:layout_height="?attr/actionbarSize"            androID:background="?attr/colorPrimary"            app:layout_scrollFlags="scroll|en@R_301_6704@lways"            app:popuptheme="@style/themeOverlay.AppCompat.Actionbar" />    </androID.support.design.Widget.AppbarLayout>    <FrameLayout        androID:ID="@+ID/content_frame"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        app:layout_behavior="@string/appbar_scrolling_vIEw_behavior"        /></androID.support.design.Widget.CoordinatorLayout><androID.support.design.Widget.NavigationVIEw    androID:ID="@+ID/navigation_vIEw"    androID:layout_height="match_parent"    androID:layout_wIDth="wrap_content"    androID:layout_gravity="start"    app:menu="@menu/menu_navigation"/>

因此,每次我从导航抽屉中选择一个项目时,我都会将FrameVIEw @ ID / content_frame替换为新的Fragment,如下所示:

int ID = menuItem.getItemID();            switch (ID)            {                case R.ID.gazzette:                    fragmentManager.beginTransaction()                            .replace(R.ID.content_frame,new ListVIEwFragment())                            .commit();                    drawerLayout.closeDrawers();                    break;                case R.ID.concorsi:                    // Insert the fragment by replacing any existing fragment                    fragmentManager.beginTransaction()                            .replace(R.ID.content_frame,new ConcorsiFragment())                            .commit();                    drawerLayout.closeDrawers();                    break;

一切正常,我有一个正确的高度工具栏.

但是当我尝试用带有TabLayout和VIEw Pager的新片段替换@ ID / content_frame Fragment时,我看到Home Activity的工具栏的高程.

这个内部片段的布局是:

<androID.support.design.Widget.CoordinatorLayoutxmlns: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:fitsSystemwindows="true"><androID.support.design.Widget.AppbarLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content">    <androID.support.design.Widget.TabLayout        androID:ID="@+ID/tabs"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        app:tabMode="fixed"        app:tabGravity="fill"        androID:elevation="6dp"        androID:background="@color/colorPrimary"/>    <androID.support.v4.vIEw.VIEwPager        androID:ID="@+ID/vIEwpager"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:background="#FFFFFF"        app:layout_behavior="@string/appbar_scrolling_vIEw_behavior"/></androID.support.design.Widget.AppbarLayout>

当然,如果我使用tableLayout启动一个新的Activity(没有片段)一切正常,但我不想创建一个新的Activity并使用选择的Navigation Drawer启动它但我只想使用Fragment项目.

如果来自HomeActivity的set app:elevation =“0dp”我在所有Application Fragment中都没有阴影.

这是在TabLayout中添加片段的正确方法吗?@H_419_14@有没有办法在这种情况下删除工具栏的阴影?

解决方法 我有解决方案,但不优雅.只需从onVIEwCreated()中的工具栏中删除高程,然后将其设置回onDestroyVIEw().

private Toolbar toolbar;private float toolbarElevation;@OverrIDepublic voID onVIEwCreated(VIEw vIEw,@Nullable Bundle savedInstanceState) {    super.onVIEwCreated(vIEw,savedInstanceState);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP) {        toolbar = getActivity().findVIEwByID(R.ID.toolbar);        toolbarElevation = toolbar.getElevation();        toolbar.setElevation(0);    }}@OverrIDepublic voID onDestroyVIEw() {    super.onDestroyVIEw();    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP) {        toolbar.setElevation(toolbarElevation);    }}
总结

以上是内存溢出为你收集整理的android – 使用TabLayout添加片段时的Material Design高程问题全部内容,希望文章能够帮你解决android – 使用TabLayout添加片段时的Material Design高程问题所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1123851.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存