【问题标题】:Material Design elevation issue when add a fragment with TabLayout使用 TabLayout 添加片段时的材料设计高程问题
【发布时间】:2017-01-01 23:31:41
【问题描述】:

我目前正在开发 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。 这是activity_home.xml 布局:

<android.support.v4.widget.DrawerLayout
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: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|enterAlways"
            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 替换为这样的新片段:

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 的新 Fragment 替换 @+id/content_frame Fragment 时,我看到了 Home Activity 工具栏的高度。

这个内部Fragment的布局是:

<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="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(没有 Fragment),一切正常,但我不想创建一个新 Activity 并通过选择 Navigation Drawer 启动它,但 我只想使用 Fragment 项目.

如果来自 HomeActivity 的一组 app:elevation="0dp" 我在所有应用程序片段中都没有影子。

这是在里面添加带有 TabLayout 的 Fragment 的正确方法吗? 有没有办法只在这种情况下去除工具栏的阴影?

【问题讨论】:

    标签: android fragment material-design android-tablayout android-elevation


    【解决方案1】:

    我有解决方案,但不优雅。只需在 onViewCreated() 中从工具栏中删除高程,然后在 onDestroyView() 中将其重新设置。

    private Toolbar toolbar;
    private float toolbarElevation;
    
    @Override
    public 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);
        }
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar.setElevation(toolbarElevation);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-12
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 2016-03-22
      • 2019-02-02
      相关资源
      最近更新 更多