android 实现沉浸式,状态栏
2018-03-21 10:16
120 查看
第一步, 配置辅助类
public class ConfigUtils {
}
8d11
示例代码调用
public class TopBarView extends BaseActivity {
}
效果展示:
public class ConfigUtils {
private ConfigUtils() { } /** * 获取状态栏高度![这里写图片描述](//img-blog.csdn.net/20180321101454292?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNzg5MDcz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) */ public static final int getStatusHeight(Context context) { int result = 0; int resId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resId > 0) { result = context.getResources().getDimensionPixelOffset(resId); } return result; } /** * 判断是否是4.4以上版本, 只有4.4以上版本才有沉浸式 */ public static boolean hasKitKat() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } /** * 由于全屏时转非全屏时界面中的布局大小不能改变,所以不能使用流行的在windows中添加一个 * Status 高度的控件来填充内容, 直接使用toolbar设置padding效果 * @param activity * @param toolbar */ public static void setStatusBarColor(Activity activity, Toolbar toolbar) { if (toolbar.getTag() == null) { ViewGroup.LayoutParams params = toolbar.getLayoutParams(); int statusHeight = getStatusHeight(activity); params.height += statusHeight; toolbar.setLayoutParams(params); toolbar.setPadding(toolbar.getPaddingLeft(), toolbar.getPaddingTop() + statusHeight, toolbar.getPaddingRight(), toolbar.getPaddingBottom()); toolbar.setTag(true); //toolbar默认有16dp的margin值,可以设置为0,或在布局中配置 toolbar.setContentInsetsRelative(0,0); } if (hasKitKat()) { //设置显示toolbar时的样式 activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } }
}
8d11
示例代码调用
public class TopBarView extends BaseActivity {
private Toolbar mToolbar; private View iv; private boolean isfull; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_top_bar_view); mToolbar = findView(R.id.m_tool_bar); iv=findViewById(R.id.iv); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (isfull){ full(false); isfull=false; }else { full(true); isfull=true; } } }); ConfigUtils.setStatusBarColor(TopBarView.this,mToolbar); initActionBar(); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); } private void full(boolean enable) { if (!enable){ //非全屏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); mToolbar.clearAnimation(); Animation animation = AnimationUtils.loadAnimation(TopBarView.this, R.anim.top_translate_enter); mToolbar.setAnimation(animation); ConfigUtils.setStatusBarColor(TopBarView.this, mToolbar); mToolbar.setVisibility(View.VISIBLE); animation.start(); }else{ mToolbar.clearAnimation(); Animation animation = AnimationUtils.loadAnimation(this, R.anim.top_translate_exit); mToolbar.setAnimation(animation); animation.start(); mToolbar.setVisibility(View.GONE); //清除非全屏时的flags,并设置全屏flag getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); } } private void initActionBar() { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); setSupportActionBar(mToolbar); ActionBar bar = getSupportActionBar(); bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); View actionView = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null); ActionBar.LayoutParams actionBarParams = new ActionBar.LayoutParams( ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); bar.setCustomView(actionView, actionBarParams); }
}
效果展示:
相关文章推荐
- android沉浸式状态栏的实现
- Android KITKAT 以上实现沉浸式状态栏
- Android 实现沉浸式状态栏
- Android 实现沉浸式状态栏的方法
- 教程分享:如何实现Android沉浸式状态栏——教你让你的状态栏变个色!
- android沉浸式状态栏的实现
- Android KITKAT 以上实现沉浸式状态栏
- 【FastDev4Android框架开发】Android实现沉浸式状态栏(六)
- (原创)Android 沉浸式状态栏实现(来自FlymeAPI)
- Android 状态栏沉浸式的实现
- 两句话,实现android 4.4以上实现沉浸式状态栏
- android 4.4以上可以实现的沉浸式状态栏效果
- android沉浸式状态栏实现
- 教程分享:如何实现Android沉浸式状态栏——教你让你的状态栏变个色!
- Android透明状态栏和沉浸式状态栏实现
- Android沉浸式状态栏的实现
- Android 4.4沉浸式状态栏的实现
- Android沉浸式状态栏简单实现
- android沉浸式状态栏的实现
- android如何实现沉浸式状态栏