我已经从Android Studio Templets创建了一个具有滚动活动功能的应用程序,以测试我对主应用程序的编码是否影响该行为,所以我只添加了代码:
I have Created an app with Scrolling Activity from the Android Studio Templets to test my coding of my main App affects that behavior or not, so I have just add to the code :
<item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item>该快照屏幕截图显示了导航栏后面的Snackbar(PS:我正在使用Xstane重新设计手机上的导航栏,但我认为这不会影响代码,因为我尝试了不带CollapsingToolbarLayout的Snackbar的TranslucentNavigation,并且可以正常工作很好)
that shows the Snackbar behind the navigation bar as this screenshot (PS : I am using Xstane for redesigning my Navigation bar on my mobile but I think that does not affect the code cuz i have tried TranslucentNavigation with Snackbar Without CollapsingToolbarLayout and that works well)
该应用程序正在支持
- windowTranslucentNavigation
- 小吃吧
- CollapsingToolbarLayout
- FloatingActionButton
这是主要xml的代码
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="schemas.android/apk/res/android" xmlns:app="schemas.android/apk/res-auto" xmlns:tools="schemas.android/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".testscanbarwithcollapsing.ScrollingActivity" > <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" android:layout_width="match_parent" android:layout_height="@dimen/app_bar_height" android:fitsSystemWindows="true" android:theme="@style/AppTheme.AppBarOverlay" > <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/toolbar_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_scrolling"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar" app:layout_anchorGravity="bottom|end" app:srcCompat="@android:drawable/ic_dialog_email" />
UPDATED:这是显示Snackbar的FloatingButton的onClick的代码(此代码位于主要活动的onCreate中)
UPDATED : here is the code of the onClick of the FloatingButton that show the Snackbar (this code is in onCreate of the main activity)
fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { fabProgressCircle.show(); Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });推荐答案
private static void show(final Activity activity, Snackbar snackbar) { if (ScreenUtils.isTranslucentNavigationBar(activity)){ final FrameLayout snackBarView = (FrameLayout) snackbar.getView(); final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) snackBarView.getChildAt(0).getLayoutParams(); params.setMargins(params.leftMargin, params.topMargin, params.rightMargin, params.bottomMargin + ScreenUtils.getNavigationBarHeight(activity)); snackBarView.getChildAt(0).setLayoutParams(params); } snackbar.show(); } public static boolean isTranslucentNavigationBar(Activity activity) { Window w = activity.getWindow(); WindowManager.LayoutParams lp = w.getAttributes(); int flags = lp.flags; return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && (flags & WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION) == WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; }
这个理想对我有用!
更多推荐
带有半透明导航的Snackbar和CollapsingToolbarLayout
发布评论