3
添加一個片段

我目前正在爲V22的應用程序,並使用下列庫:材質設計高程的問題時TabLayout

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_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; 

一切正常很好,我有一個正確的海拔高度的工具欄。

right elevation

但是當我嘗試用一​​個TabLayout和查看傳呼機我看到首頁活動的工具欄的高度新的片段,以取代@+id/content_frame片段。

enter image description here

這種內在的片段的佈局是:

<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一切正常的話,但我不要創建一個新的活動並啓動它與選擇導航抽屜,但我只想使用片段項目。

如果來自HomeActivity的集合app:elevation="0dp"我在所有Application Fragment中都沒有陰影。

這是添加TabLayout裏面的片段的正確方法? 只有在這種情況下才有辦法去除工具欄的陰影嗎?

回答

0

我有解決方案,但不優雅。只需從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); 
    } 
}