Dam*_*ano 5 android fragment material-design android-elevation android-tablayout
我目前正在开发 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'
Run Code Online (Sandbox Code Playgroud)
我只有一个名为HomeActivity 的活动,里面有一个导航抽屉。这是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"/>
Run Code Online (Sandbox Code Playgroud)
因此,每次我从导航抽屉中选择一个项目时,我都会@+id/content_frame用一个新的 Fragment替换 FrameView ,如下所示:
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;
Run Code Online (Sandbox Code Playgroud)
一切正常,我有一个正确高度的工具栏。
但是当我尝试@+id/content_frame用带有TabLayout和 View Pager的新 Fragment替换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>
Run Code Online (Sandbox Code Playgroud)
自然地,如果我使用 TableLayout 启动一个新的 Activity(没有 Fragment),一切正常,但我不想创建一个新的 Activity 并使用一个 Navigation Drawer 的选择启动它,但我只想使用 Fragment 项目。
如果app:elevation="0dp"来自 HomeActivity的集合,我在所有应用程序片段中都没有阴影。
这是在里面添加带有 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);
}
}
Run Code Online (Sandbox Code Playgroud)