gpt4 book ai didi

android - 使用 TabLayout 添加 fragment 时的 Material Design 高程问题

转载 作者:太空宇宙 更新时间:2023-11-03 11:02:37 25 4
gpt4 key购买 nike

我目前正在开发 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,里面有一个抽屉导航。这是 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"/>

因此,每次我从抽屉导航中选择一个项目时,我都会用这样的新 fragment 替换 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;

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

right elevation

但是,当我尝试用带有 TabLayout 和 View Pager 的新 fragment 替换 @+id/content_frame fragment 时,我看到主页工具栏的高度 Activity 。

enter image description here

这个内部 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" 我在所有应用程序 fragment 中都没有阴影。

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

最佳答案

我有解决方案,但不够优雅。只需在 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);
}
}

关于android - 使用 TabLayout 添加 fragment 时的 Material Design 高程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39146881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com