android 两种方式实现沉浸式菜单在项目中所遇到的一些问题
2016-09-08 18:19
519 查看
效果图:
如果想改变返回键的图片,我们也可以在代码中进行处理:
可以看出这里并没有设置
actionBar.setHomeButtonEnabled(true);
也即是返回键可用,因为我们这里实现的是一个侧滑的效果。下面会有讲到。
首先,还是对这方面的知识(就自己目前所理解的来写)做一个总结。
我们如果要用AppCompatActivity来实现沉浸式菜单栏,个人觉得需要注意/了解的点:
1.因为在androidstuido中创建一个project之后,布局里面会自动生成一个toolbar。但是在此之前我们应该要知道,系统默认(不知道是不是应该叫做系统默认)的会有一个actionBar,如果我们在布局里面再写一个toolbar,就相当于是重复加了,创建project之后,直接运行项目之所以没有报错是因为我们在AndroidMainifest.xml中配置了android:theme=”@style/AppTheme.NoActionBar”
也即是去掉了系统默认actionBar
可以styles.xml从代码中看出
windowActionBar 设置为 false 就是不使用默认的actionBar
windowNoTitle 设置为 true 就是不使用标题
2.至于标题栏,目前还不能够用代码来设置它的位置,所以如果我们需要标题居中或者是其它,我们可以自己定义一个布局,来设置标题的显示位置。
如下:
3.自带的返回键的使用
这样写的话,我们仅可以看到是返回键的显示,但是点击效果却没有实现。
我们可以在公共的Activity中,如:BaseActivity中去添加一个这样的方法:
这样说,肯定会有人不理解,附上一个参考案例,
http://blog.csdn.net/flowingflying/article/details/14001855
好了步入正题:
先说实现步骤:
1.加依赖:
2.在布局文件中进行书写(参考上面的那个布局就可以了)
至于属性:
android:fitsSystemWindows=”true”从网上看到的是说,加在最外层的布局中,因为我做项目的时候,刚开始用到的是 用样式来实现的,所以toolbar中也加的有,不过经过测试,如果在代码中实现的话,也不会影响效果。只针对最外层起作用的原因。
3.最重要的我觉得就是样式的配置了
在此,我简单的提一下吧。
因为我们用了toolbar,所以样式中的那两个属性
肯定要是全局的(在AndroidMainifest.xml中)
顺便解释一下:
colorPrimary:标题栏的颜色
colorPrimaryDark:状态栏的颜色
colorAccent :如果布局中有用到复选框之类的控件的话,打勾的时候设置的颜色
说一下我的实现思路吧。
第一种:(代码中进行控制)个人所参考的链接地址
http://blog.csdn.net/lplj717/article/details/52456708
相关内容CoordinatorLayout 介绍的链接:http://blog.csdn.net/xyz_lmn/article/details/48055919
我就从我所参考的案例中,来阐释我所能够看到的东西。
代码进行控制,我主要就从两点来说:
1.就是属性:android:fitsSystemWindows=”true”在布局文件中的使用,千万不要忘记
2.用到一个类:(上面有案例提到了,我也列一下吧,供以后自己参考)
3.也就是在需要用到的activity中进行判断及调用。(一般都是写在公共的BaseActivity中)
第二种:
也即是在style中进行控制以及这个属性 android:fitsSystemWindows=”true”之间的使用。
可能会涉及到values-19 或者values-21之类的,不过我觉得你只要把样式跟这个属性使用好(或者是弄明白)之后,就可以了,需要让我们写的东西并不多。
到此基本上就结束了,可能有的人会在别的博客里面看到关于4.0或者是5.0实现效果的一些差异,以及一些解决方法,我在此就不多说了。
因为我在项目中需要实现一个功能就是在首页,标题栏就需要设置成透明或者是白色,也即是隐藏的效果,我用第二种方法实现的效果并不是很理想,后来就改为来第一种。如果有需要实现这个效果的小伙伴们可以在样式里面在写一个样式:colorPrimary颜色可以设置从透明的颜色:
然后针对相应的activity,在AndroidMainifest.xml中进行配置。
还有就是app的引导页可能不需要继承Baseactivity(当然Baseactivity
继承AppcompatActivity),所以不会有设置屏幕全屏的烦恼。
但是有的地方会烦恼,比如说没有toolbar的地方:
因为在代码中进行控制,所以BaseActivity中设置的
会影响实现效果,它会在状态栏显示这个颜色,很难看。我也不知道该怎么设置它不见,但是我们可以在没有toolbar的activity中设置
有不对的地方,还请小伙伴们,指教说明。谢谢
如果想改变返回键的图片,我们也可以在代码中进行处理:
final ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { actionBar.setHomeAsUpIndicator(R.mipmap.main); actionBar.setDisplayHomeAsUpEnabled(true); }
可以看出这里并没有设置
actionBar.setHomeButtonEnabled(true);
也即是返回键可用,因为我们这里实现的是一个侧滑的效果。下面会有讲到。
首先,还是对这方面的知识(就自己目前所理解的来写)做一个总结。
我们如果要用AppCompatActivity来实现沉浸式菜单栏,个人觉得需要注意/了解的点:
1.因为在androidstuido中创建一个project之后,布局里面会自动生成一个toolbar。但是在此之前我们应该要知道,系统默认(不知道是不是应该叫做系统默认)的会有一个actionBar,如果我们在布局里面再写一个toolbar,就相当于是重复加了,创建project之后,直接运行项目之所以没有报错是因为我们在AndroidMainifest.xml中配置了android:theme=”@style/AppTheme.NoActionBar”
也即是去掉了系统默认actionBar
可以styles.xml从代码中看出
<style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style>
windowActionBar 设置为 false 就是不使用默认的actionBar
windowNoTitle 设置为 true 就是不使用标题
2.至于标题栏,目前还不能够用代码来设置它的位置,所以如果我们需要标题居中或者是其它,我们可以自己定义一个布局,来设置标题的显示位置。
如下:
<LinearLayout 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:layout_width="match_parent" android:layout_height="60dp" android:orientation="vertical" tools:context=".MainActivity" <!--这句很重要--> android:fitsSystemWindows="true" android:background="?attr/colorPrimaryDark" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:theme="@style/AppTheme.AppBarOverlay" app:popupTheme="@style/AppTheme.PopupOverlay" <!--这句很重要--> android:fitsSystemWindows="true" > <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:textColor="@android:color/white" android:textSize="20sp" /> </android.support.v7.widget.Toolbar> </LinearLayout>
3.自带的返回键的使用
package demo.packg.myapplication; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.TextView; public class MainActivity extends BaseActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv1= (TextView) findViewById(R.id.text1); //因为MainActivity在AndroidMainifest中,配置了去掉系统自带的标题栏,所以加了一个toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); initActionBar(); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,MainActivity2.class)); } }); } public void initActionBar() { setSupportActionBar(toolbar); final ActionBar actionBar = getSupportActionBar(); //设置返回键可用 actionBar.setHomeButtonEnabled(true); actionBar.setDisplayShowHomeEnabled(true);//这两句就可以让actionBar的图标可以响应点击事件 actionBar.setDisplayHomeAsUpEnabled(true);//这一句主要用于后面返回效果,后面会讲 //由于标题栏名字的位置不能更改,所以想要更改标题栏的位置,就得自己自定义布局文件 actionBar.setTitle("首页"); //如果自己定义了标题栏,那么我们可以在这里设置标题栏为空 actionBar.setTitle(null); } }
这样写的话,我们仅可以看到是返回键的显示,但是点击效果却没有实现。
我们可以在公共的Activity中,如:BaseActivity中去添加一个这样的方法:
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if (item.getItemId() == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); }
这样说,肯定会有人不理解,附上一个参考案例,
http://blog.csdn.net/flowingflying/article/details/14001855
好了步入正题:
先说实现步骤:
1.加依赖:
compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.4.0'
2.在布局文件中进行书写(参考上面的那个布局就可以了)
至于属性:
android:fitsSystemWindows=”true”从网上看到的是说,加在最外层的布局中,因为我做项目的时候,刚开始用到的是 用样式来实现的,所以toolbar中也加的有,不过经过测试,如果在代码中实现的话,也不会影响效果。只针对最外层起作用的原因。
3.最重要的我觉得就是样式的配置了
在此,我简单的提一下吧。
因为我们用了toolbar,所以样式中的那两个属性
<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
肯定要是全局的(在AndroidMainifest.xml中)
顺便解释一下:
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item>
colorPrimary:标题栏的颜色
colorPrimaryDark:状态栏的颜色
colorAccent :如果布局中有用到复选框之类的控件的话,打勾的时候设置的颜色
说一下我的实现思路吧。
第一种:(代码中进行控制)个人所参考的链接地址
http://blog.csdn.net/lplj717/article/details/52456708
相关内容CoordinatorLayout 介绍的链接:http://blog.csdn.net/xyz_lmn/article/details/48055919
我就从我所参考的案例中,来阐释我所能够看到的东西。
代码进行控制,我主要就从两点来说:
1.就是属性:android:fitsSystemWindows=”true”在布局文件中的使用,千万不要忘记
2.用到一个类:(上面有案例提到了,我也列一下吧,供以后自己参考)
package demo.packg.myapplication; import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.app.Activity; import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.os.Build; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.Gravity; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; import java.lang.reflect.Method; /** * Created by Developer_02 on 2016/9/7. */ public class SystemBarTintManager { static { // Android allows a system property to override the presence of the navigation bar. // Used by the emulator. // See https://github.com/android/platform_frameworks_base/blob/master/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java#L1076 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try { Class c = Class.forName("android.os.SystemProperties"); Method m = c.getDeclaredMethod("get", String.class); m.setAccessible(true); sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys"); } catch (Throwable e) { sNavBarOverride = null; } } } /** * The default system bar tint color value. */ public static final int DEFAULT_TINT_COLOR = 0x99000000; private static String sNavBarOverride; private final SystemBarConfig mConfig; private boolean mStatusBarAvailable; private boolean mNavBarAvailable; private boolean mStatusBarTintEnabled; private boolean mNavBarTintEnabled; private View mStatusBarTintView; private View mNavBarTintView; /** * Constructor. Call this in the host activity onCreate method after its * content view has been set. You should always create new instances when * the host activity is recreated. * * @param activity The host activity. */ @TargetApi(19) public SystemBarTintManager(Activity activity) { Window win = activity.getWindow(); ViewGroup decorViewGroup = (ViewGroup) win.getDecorView(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // check theme attrs int[] attrs = {android.R.attr.windowTranslucentStatus, android.R.attr.windowTranslucentNavigation}; TypedArray a = activity.obtainStyledAttributes(attrs); try { mStatusBarAvailable = a.getBoolean(0, false); mNavBarAvailable = a.getBoolean(1, false); } finally { a.recycle(); } // check window flags WindowManager.LayoutParams winParams = win.getAttributes(); int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if ((winParams.flags & bits) != 0) { mStatusBarAvailable = true; } bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION; if ((winParams.flags & bits) != 0) { mNavBarAvailable = true; } } mConfig = new SystemBarConfig(activity, mStatusBarAvailable, mNavBarAvailable); // device might not have virtual navigation keys if (!mConfig.hasNavigtionBar()) { mNavBarAvailable = false; } if (mStatusBarAvailable) { setupStatusBarView(activity, decorViewGroup); } if (mNavBarAvailable) { setupNavBarView(activity, decorViewGroup); } } /** * Enable tinting of the system status bar. * * If the platform is running Jelly Bean or earlier, or translucent system * UI modes have not been enabled in either the theme or via window flags, * then this method does nothing. * * @param enabled True to enable tinting, false to disable it (default). */ public void setStatusBarTintEnabled(boolean enabled) { mStatusBarTintEnabled = enabled; if (mStatusBarAvailable) { mStatusBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE); } } /** * Enable tinting of the system navigation bar. * * If the platform does not have soft navigation keys, is running Jelly Bean * or earlier, or translucent system UI modes have not been enabled in either * the theme or via window flags, then this method does nothing. * * @param enabled True to enable tinting, false to disable it (default). */ public void setNavigationBarTintEnabled(boolean enabled) { mNavBarTintEnabled = enabled; if (mNavBarAvailable) { mNavBarTintView.setVisibility(enabled ? View.VISIBLE : View.GONE); } } /** * Apply the specified color tint to all system UI bars. * * @param color The color of the background tint. */ public void setTintColor(int color) { setStatusBarTintColor(color); setNavigationBarTintColor(color); } /** * Apply the specified drawable or color resource to all system UI bars. * * @param res The identifier of the resource. */ public void setTintResource(int res) { setStatusBarTintResource(res); setNavigationBarTintResource(res); } /** * Apply the specified drawable to all system UI bars. * * @param drawable The drawable to use as the background, or null to remove it. */ public void setTintDrawable(Drawable drawable) { setStatusBarTintDrawable(drawable); setNavigationBarTintDrawable(drawable); } /** * Apply the specified alpha to all system UI bars. * * @param alpha The alpha to use */ public void setTintAlpha(float alpha) { setStatusBarAlpha(alpha); setNavigationBarAlpha(alpha); } /** * Apply the specified color tint to the system status bar. * * @param color The color of the background tint. */ public void setStatusBarTintColor(int color) { if (mStatusBarAvailable) { mStatusBarTintView.setBackgroundColor(color); } } /** * Apply the specified drawable or color resource to the system status bar. * * @param res The identifier of the resource. */ public void setStatusBarTintResource(int res) { if (mStatusBarAvailable) { mStatusBarTintView.setBackgroundResource(res); } } /** * Apply the specified drawable to the system status bar. * * @param drawable The drawable to use as the background, or null to remove it. */ @SuppressWarnings("deprecation") public void setStatusBarTintDrawable(Drawable drawable) { if (mStatusBarAvailable) { mStatusBarTintView.setBackgroundDrawable(drawable); } } /** * Apply the specified alpha to the system status bar. * * @param alpha The alpha to use */ @TargetApi(11) public void setStatusBarAlpha(float alpha) { if (mStatusBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mStatusBarTintView.setAlpha(alpha); } } /** * Apply the specified color tint to the system navigation bar. * * @param color The color of the background tint. */ public void setNavigationBarTintColor(int color) { if (mNavBarAvailable) { mNavBarTintView.setBackgroundColor(color); } } /** * Apply the specified drawable or color resource to the system navigation bar. * * @param res The identifier of the resource. */ public void setNavigationBarTintResource(int res) { if (mNavBarAvailable) { mNavBarTintView.setBackgroundResource(res); } } /** * Apply the specified drawable to the system navigation bar. * * @param drawable The drawable to use as the background, or null to remove it. */ @SuppressWarnings("deprecation") public void setNavigationBarTintDrawable(Drawable drawable) { if (mNavBarAvailable) { mNavBarTintView.setBackgroundDrawable(drawable); } } /** * Apply the specified alpha to the system navigation bar. * * @param alpha The alpha to use */ @TargetApi(11) public void setNavigationBarAlpha(float alpha) { if (mNavBarAvailable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { mNavBarTintView.setAlpha(alpha); } } /** * Get the system bar configuration. * * @return The system bar configuration for the current device configuration. */ public SystemBarConfig getConfig() { return mConfig; } /** * Is tinting enabled for the system status bar? * * @return True if enabled, False otherwise. */ public boolean isStatusBarTintEnabled() { return mStatusBarTintEnabled; } /** * Is tinting enabled for the system navigation bar? * * @return True if enabled, False otherwise. */ public boolean isNavBarTintEnabled() { return mNavBarTintEnabled; } private void setupStatusBarView(Context context, ViewGroup decorViewGroup) { mStatusBarTintView = new View(context); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, mConfig.getStatusBarHeight()); params.gravity = Gravity.TOP; if (mNavBarAvailable && !mConfig.isNavigationAtBottom()) { params.rightMargin = mConfig.getNavigationBarWidth(); } mStatusBarTintView.setLayoutParams(params); mStatusBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR); mStatusBarTintView.setVisibility(View.GONE); decorViewGroup.addView(mStatusBarTintView); } private void setupNavBarView(Context context, ViewGroup decorViewGroup) { mNavBarTintView = new View(context); FrameLayout.LayoutParams params; if (mConfig.isNavigationAtBottom()) { params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, mConfig.getNavigationBarHeight()); params.gravity = Gravity.BOTTOM; } else { params = new FrameLayout.LayoutParams(mConfig.getNavigationBarWidth(), FrameLayout.LayoutParams.MATCH_PARENT); params.gravity = Gravity.RIGHT; } mNavBarTintView.setLayoutParams(params); mNavBarTintView.setBackgroundColor(DEFAULT_TINT_COLOR); mNavBarTintView.setVisibility(View.GONE); decorViewGroup.addView(mNavBarTintView); } /** * Class which describes system bar sizing and other characteristics for the current * device configuration. * */ public static class SystemBarConfig { private static final String STATUS_BAR_HEIGHT_RES_NAME = "status_bar_height"; private static final String NAV_BAR_HEIGHT_RES_NAME = "navigation_bar_height"; private static final String NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME = "navigation_bar_height_landscape"; private static final String NAV_BAR_WIDTH_RES_NAME = "navigation_bar_width"; private static final String SHOW_NAV_BAR_RES_NAME = "config_showNavigationBar"; private final boolean mTranslucentStatusBar; private final boolean mTranslucentNavBar; private final int mStatusBarHeight; private final int mActionBarHeight; private final boolean mHasNavigationBar; private final int mNavigationBarHeight; private final int mNavigationBarWidth; private final boolean mInPortrait; private final float mSmallestWidthDp; private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) { Resources res = activity.getResources(); mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT); mSmallestWidthDp = getSmallestWidthDp(activity); mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME); mActionBarHeight = getActionBarHeight(activity); mNavigationBarHeight = getNavigationBarHeight(activity); mNavigationBarWidth = getNavigationBarWidth(activity); mHasNavigationBar = (mNavigationBarHeight > 0); mTranslucentStatusBar = translucentStatusBar; mTranslucentNavBar = traslucentNavBar; } @TargetApi(14) private int getActionBarHeight(Context context) { int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { TypedValue tv = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true); result = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics()); } return result; } @TargetApi(14) private int getNavigationBarHeight(Context context) { Resources res = context.getResources(); int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { if (hasNavBar(context)) { String key; if (mInPortrait) { key = NAV_BAR_HEIGHT_RES_NAME; } else { key = NAV_BAR_HEIGHT_LANDSCAPE_RES_NAME; } return getInternalDimensionSize(res, key); } } return result; } @TargetApi(14) private int getNavigationBarWidth(Context context) { Resources res = context.getResources(); int result = 0; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { if (hasNavBar(context)) { return getInternalDimensionSize(res, NAV_BAR_WIDTH_RES_NAME); } } return result; } @TargetApi(14) private boolean hasNavBar(Context context) { Resources res = context.getResources(); int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android"); if (resourceId != 0) { boolean hasNav = res.getBoolean(resourceId); // check override flag (see static block) if ("1".equals(sNavBarOverride)) { hasNav = false; } else if ("0".equals(sNavBarOverride)) { hasNav = true; } return hasNav; } else { // fallback return !ViewConfiguration.get(context).hasPermanentMenuKey(); } } private int getInternalDimensionSize(Resources res, String key) { int result = 0; int resourceId = res.getIdentifier(key, "dimen", "android"); if (resourceId > 0) { result = res.getDimensionPixelSize(resourceId); } return result; } @SuppressLint("NewApi") private float getSmallestWidthDp(Activity activity) { DisplayMetrics metrics = new DisplayMetrics(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { activity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics); } else { // TODO this is not correct, but we don't really care pre-kitkat activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); } float widthDp = metrics.widthPixels / metrics.density; float heightDp = metrics.heightPixels / metrics.density; return Math.min(widthDp, heightDp); } /** * Should a navigation bar appear at the bottom of the screen in the current * device configuration? A navigation bar may appear on the right side of * the screen in certain configurations. * * @return True if navigation should appear at the bottom of the screen, False otherwise. */ public boolean isNavigationAtBottom() { return (mSmallestWidthDp >= 600 || mInPortrait); } /** * Get the height of the system status bar. * * @return The height of the status bar (in pixels). */ public int getStatusBarHeight() { return mStatusBarHeight; } /** * Get the height of the action bar. * * @return The height of the action bar (in pixels). */ public int getActionBarHeight() { return mActionBarHeight; } /** * Does this device have a system navigation bar? * * @return True if this device uses soft key navigation, False otherwise. */ public boolean hasNavigtionBar() { return mHasNavigationBar; } /** * Get the height of the system navigation bar. * * @return The height of the navigation bar (in pixels). If the device does not have * soft navigation keys, this will always return 0. */ public int getNavigationBarHeight() { return mNavigationBarHeight; } /** * Get the width of the system navigation bar when it is placed vertically on the screen. * * @return The width of the navigation bar (in pixels). If the device does not have * soft navigation keys, this will always return 0. */ public int getNavigationBarWidth() { return mNavigationBarWidth; } /** * Get the layout inset for any system UI that appears at the top of the screen. * * @param withActionBar True to include the height of the action bar, False otherwise. * @return The layout inset (in pixels). */ public int getPixelInsetTop(boolean withActionBar) { return (mTranslucentStatusBar ? mStatusBarHeight : 0) + (withActionBar ? mActionBarHeight : 0); } /** * Get the layout inset for any system UI that appears at the bottom of the screen. * * @return The layout inset (in pixels). */ public int getPixelInsetBottom() { if (mTranslucentNavBar && isNavigationAtBottom()) { return mNavigationBarHeight; } else { return 0; } } /** * Get the layout inset for any system UI that appears at the right of the screen. * * @return The layout inset (in pixels). */ public int getPixelInsetRight() { if (mTranslucentNavBar && !isNavigationAtBottom()) { return mNavigationBarWidth; } else { return 0; } } } }
3.也就是在需要用到的activity中进行判断及调用。(一般都是写在公共的BaseActivity中)
package demo.packg.myapplication;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
/**
* Created by Developer_02 on 2016/9/7.
*/
public class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.colorPrimary);//状态栏所需颜色
}
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
//这个就是toolbar那个默认的返回键需要用到的方法
@Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if (item.getItemId() == android.R.id.home) { finish(); return true; } return super.onOptionsItemSelected(item); }
}
第二种:
也即是在style中进行控制以及这个属性 android:fitsSystemWindows=”true”之间的使用。
可能会涉及到values-19 或者values-21之类的,不过我觉得你只要把样式跟这个属性使用好(或者是弄明白)之后,就可以了,需要让我们写的东西并不多。
到此基本上就结束了,可能有的人会在别的博客里面看到关于4.0或者是5.0实现效果的一些差异,以及一些解决方法,我在此就不多说了。
因为我在项目中需要实现一个功能就是在首页,标题栏就需要设置成透明或者是白色,也即是隐藏的效果,我用第二种方法实现的效果并不是很理想,后来就改为来第一种。如果有需要实现这个效果的小伙伴们可以在样式里面在写一个样式:colorPrimary颜色可以设置从透明的颜色:
<style name="AppTheme_main" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@android:color/transparent</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="windowActionBar">false</item> <item name="windowNoTitle">true</item>
</style>
然后针对相应的activity,在AndroidMainifest.xml中进行配置。
还有就是app的引导页可能不需要继承Baseactivity(当然Baseactivity
继承AppcompatActivity),所以不会有设置屏幕全屏的烦恼。
但是有的地方会烦恼,比如说没有toolbar的地方:
因为在代码中进行控制,所以BaseActivity中设置的
SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(R.color.colorPrimary);//状态栏所需颜色
会影响实现效果,它会在状态栏显示这个颜色,很难看。我也不知道该怎么设置它不见,但是我们可以在没有toolbar的activity中设置
SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(R.color.colorPrimary);//状态栏所需颜色 不过这个颜色我们要改成透明,这样它的实现效果就是 透明的加状态栏 原本的颜色,因为BaseActivity中设置了颜色,我们又在相应的activity中设置了透明,所以是两种效果的结合体。 登录界面,想让导航栏是完全透明的效果,我们可以直接再写一个BaseLoginActivity,然后把状态栏的颜色设置成透明的就可以了,LoginActivity就不需要再设置了。 我手机是5.0以上的实现效果喔。
有不对的地方,还请小伙伴们,指教说明。谢谢
相关文章推荐
- Android底部菜单的两种实现方式
- android学习-----使用TabLayout实现Tab选项卡以及遇到的一些问题(二)
- Android DES加解密的两种实现方式 - Java DES与Android DES不兼容问题
- Android沉浸式状态栏与EditText问题【沉浸式状态栏实现及遇到的坑】
- Android过渡动画之共享元素实现以及遇到的一些问题
- 项目中遇到的Android drawText的一些问题
- Android菜单实现两种方式
- 项目遇到的一些问题 实现思路
- 新建android项目后,遇到的一些问题的解决方法:
- android学习-----使用TabLayout实现Tab选项卡以及遇到的一些问题(一)
- Android 项目需求Camera + SurfaceView实现自定义拍照,遇到的问题
- Android沉浸式状态栏的两种实现方式
- Android Studio 之 导入Eclipse项目常见问题及解决方案 在将Eclipse做的Android项目成功导入Android Studio 后,启动生成,遇到一些问题,现总结如下:
- 两种方式实现Android侧滑菜单
- Android 跑马灯效果实现的两种方式,解决和viewpager的冲突问题
- android 沉浸式状态栏的两种实现方式
- 【Android】沉浸式状态栏的两种实现方式
- [一般问题] android导入项目中遇到的一些问题及解决方法
- Android 沉浸式状态栏实现,以及遇到的问题
- android 源码编译遇到得一些问题