android 导航栏半透明,Android屏幕适配之状态栏导航栏半透明、全透明(5.0以上去阴影),方法多样...
使用方法
1.工程目录下的build.gradle中添加代码:
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
2.直接在需要使用的模块的build.gradle中添加代码:
注意:记得去gayhub查看最新版本,最新版本最niubility
implementation 'com.github.AnJiaoDe:StatusNavigationTransparent:V1.1.7'
本文只描述在activity中如何使用,fragment中使用方法类似,可以实现
首先创建全局theme
@color/colorPrimary
@color/colorPrimaryDark
@color/theme
@color/white
stateAlwaysHidden
@color/text_deep
@dimen/size_3
centerInside
singleTop
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
然后创建BaseActivity实现StatusBar全透明
public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StatusNavUtils.setStatusBarColor(this,0x00000000);
}
public void startAppcompatActivity(Class> cls) {
startActivity(new Intent(this, cls));
}
}
1. StatusBar半透明
1.1 StatusBar半透明用StatusBarView实现(4.4以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#33000000" />
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="StatusBar半透明(statusBarView)"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
1.2 StatusBar半透明用setStatusBarColor实现(5.0以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"style="@style/transparent_statusbar_fit"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="StatusBar半透明(setStatusBarColor)"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
public class Status2Activity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status2);
StatusNavUtils.setStatusBarColor(this,0x33000000);
}
@Override
public void onClick(View v) {
}
}
2. StatusBar全透明
2.1 StatusBar全透明用fitSystemWindows实现(4.4以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"style="@style/transparent_statusbar_fit"
android:background="@color/colorPrimary"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="StatusBar全透明(fitsystemwindows)"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
2.2 StatusBar全透明用StatusBarView实现(4.4以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000" />
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="StatusBar全透明(StatusBarView)"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
3. StatusBar透明于图片之上
3.1 StatusBar半透明于图片之上用setStatusBarColor实现(5.0以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/huge" />
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="StatusBar半透明于图片之上(setStatusBarColor)"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
public class Status5Activity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status5);
StatusNavUtils.setStatusBarColor(this,0x33000000);
}
@Override
public void onClick(View v) {
}
}
3.2 StatusBar全透明于图片之上(4.4以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/huge" />
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="StatusBar全透明于图片之上"
android:textColor="#ffffff" />
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
3. NavigationBar透明
3.1 NavigationBar半透明用setNavigationBarColor实现(5.0以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_status7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="navigationBar半透明(setNavigationBarColor)"
android:textColor="@color/white" />
public class Navigation1Activity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_status7);
StatusNavUtils.setNavigationBarColor(this,0x33000000);
}
@Override
public void onClick(View v) {
}
}
3.2 NavigationBar全透明(4.4以上有效)
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_status7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:text="navigationBar全透明"
android:textColor="@color/white" />
public class Navigation2Activity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation2);
StatusNavUtils.setNavigationBarColor(this,0x00000000);
}
@Override
public void onClick(View v) {
}
}
源码:
transparent_statusbar_fit
true
false
StatusNavUtils
public class StatusNavUtils {
/**
* 状态栏透明去阴影(5.0以上)
*
* @param activity
* @param color
*/
public static void setStatusBarColor(Activity activity, int color) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
/**
* 导航栏全透明去阴影(5.0以上)
*
* @param activity
* @param color
*/
public static void setNavigationBarColor(Activity activity, int color) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setNavigationBarColor(color);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* 状态栏、导航栏全透明去阴影(5.0以上)
*
* @param activity
* @param color_status
* @param color_nav
*/
public static void setStatusNavBarColor(Activity activity, int color_status, int color_nav) {
Window window = activity.getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(color_status);
window.setNavigationBarColor(color_nav);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
}
StatusBarView
public class StatusBarView extends View {
private Context context;
public StatusBarView(Context context) {
this(context,null);
}
public StatusBarView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context=context;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(ScreenUtils.getStatusBarHeight(context), MeasureSpec.EXACTLY));
}
}
ScreenUtils
public class ScreenUtils {
public static int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}
public static int getNavigationBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
return resources.getDimensionPixelSize(resourceId);
}
public static int getScreenWidth(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService("window");
if (Build.VERSION.SDK_INT <= 17) {
wm.getDefaultDisplay().getMetrics(displayMetrics);
} else {
wm.getDefaultDisplay().getRealMetrics(displayMetrics);
}
return displayMetrics.widthPixels;
}
public static int getScreenHeight(Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) context.getSystemService("window");
if (Build.VERSION.SDK_INT <= 17) {
wm.getDefaultDisplay().getMetrics(displayMetrics);
} else {
wm.getDefaultDisplay().getRealMetrics(displayMetrics);
}
return displayMetrics.heightPixels;
}
public static int setYStart(Context context, float y) {
// if (isGroove(context)) {
return (int) (y * getScreenHeight(context)) + getStatusBarHeight(context);
// } else {
// return (int) (y * getScreenHeight(context));
//
//
// }
}
/**
* 获取当前界面可视区域的高度
*
* @param activity
* @return
*/
public static int getVisibleFrameHeight(Activity activity) {
Rect r = new Rect();
//获取当前界面可视部分
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
// if (isGroove(activity)) {
// return r.bottom - r.top - getStatusBarHeight(activity);
// } else {
return r.bottom - r.top;
//
// }
}
/**
* 获取当前界面可视区域的宽度
*
* @param activity
* @return
*/
public static int getVisibleFrameWidth(Activity activity) {
Rect r = new Rect();
//获取当前界面可视部分
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
return r.right - r.left;
}
}
欢迎联系、指正、批评
微信公众号
QQ群
本文地址:https://blog.csdn.net/confusing_awakening/article/details/107955105
android 导航栏半透明,Android屏幕适配之状态栏导航栏半透明、全透明(5.0以上去阴影),方法多样...相关推荐
- Android高级-网易云音乐屏幕适配
为什么要进行屏幕适配 屏幕碎片化表现为以下几个方面: 1:屏幕尺寸碎片化: 2:屏幕密度碎片化 3:厂商碎片化:水滴屏,刘海屏 屏幕适配常见方式 二 谷歌推出的百分比布局 手写百分比布局; 为什么百分 ...
- android 魅族屏幕适配,Flyme 9发布:首发小窗模式3.0 魅族18系列首先适配
Flyme 9发布:首发小窗模式3.0 魅族18系列首先适配 2021-03-02 19:25:12 0点赞 0收藏 0评论 [科技犬] 此次Flyme 9秉承着"轻盈有序"的设计 ...
- android 贝塞尔曲线 人脸,贝塞尔曲线绘制人脸框(框内全透明,框外半透明)
参考CropImage 制作截取头像框https://github.com/cokeduo/CropImage https://www.jianshu.com/p/c883fbf52681 //贝塞尔 ...
- android 导航栏半透明,Android 沉浸式/透明式状态栏、导航栏
前言 Android 从4.4开始引进透明状态栏和导航栏的概念,并且在5.0进行了改进,将透明变成了半透明的效果.虽然此特性最早出现在ios,但不否认效果还是很赞的. 至于4.4以下的手机,就不要考虑 ...
- Android 屏幕尺寸、屏幕分辨率(px)、屏幕像素密度(dpi)、密度无关像素(dp/dip)、屏幕适配
1. dp , dpi , px 关系 (假设已经熟悉了基本概念,也可以参考最后的链接) 2. 运行时dp 如何转换成 px 3. 使用dp 和 px 设计结果比较 参考: Android屏幕适 ...
- 【Android】之屏幕适配
前言 Android 系统能发展到今天,离不开其开源性,但是随着越来越多的设备接入 Android 系统,并对 Android 系统进行各种各样的定制,导致长期以来出现了各种碎片化严重的问题.例如,A ...
- 高级Android研发面试必问:Android屏幕适配全方位解析
前言 前面我们已经将android的绘制基础已经讲完,那么现在我们下面的两个内容点是事件分发问题,和屏幕适配相关.这篇我们主要来进android但中的各种屏幕适配问题 1.屏幕适配概念 而随着支持An ...
- Cocos2D-X屏幕适配新解
为了适应移动终端的各种分辨率大小,各种屏幕宽高比,在 Cocos2D-X(当前稳定版:2.0.4) 中,提供了相应的解决方案,以方便我们在设计游戏时,能够更好的适应不同的环境. 而在设计游戏之初,决定 ...
- android自适应拉伸图片,Android 启动页-解决图片被拉伸和压缩问题,适配虚拟导航栏...
Android 启动页设置非常简单 //styles.xml 设置主题 @drawable/bg_splash true //activity使用主题,这时点击app图标,就会显示@drawable/ ...
- android界面UI美化:沉浸模式、全透明或半透明状态栏及导航栏的实现
android api19开始我们就能对顶部状态栏和底部导航栏进行半透明处理了,而api21开始则可以实现全透明状态栏与导航栏以及开启沉浸模式,至于什么是沉浸模式,大家百度一下应该就都知道了,有一点需 ...
最新文章
- npm和yarn的区别,我们该如何选择?
- Vue在渲染函数createELement和JSX中使用插槽slot
- mysql mybatis list循环_mybatis框架,使用foreach实现复杂结果的查询--循环List集合方式...
- 常见Java开发过程中遇到的问题及其解决办法
- (一)问候 Log4j 你好
- Python学习-基础篇14 Web框架本质及第一个Django实例
- pandas—pandas.DataFrame.query与pandas.DataFrame.reset_index
- 系统没有安装vc9.注意是x86 32位_x86是多少位系统
- 计算机网络——网络安全
- 2021认证杯(小美赛)思路分析
- 图的最短路径之Dijkstra算法
- 【慕课笔记】第一章 JAVA初体验 第3节 使用记事本编写JAVA程序
- 笔记本怎么自己装系统?u盘装系统windows7教程图解
- python求和1到100_python求1到100的和的几种方法
- “随机梯度下降、牛顿法、动量法、Nesterov、AdaGrad、RMSprop、Adam”
- 2021年全球支付现状及发展趋势分析:亚太电子商务数字支付将超过3.1万亿美元[图]
- 31岁零基础转型开发,目前35岁,说说我转行的辛酸史
- 加速度计和陀螺仪区别
- HP笔记本电池充放电
- 无人机真正射影像的概念和制作原理
热门文章
- 莫名其妙的解决了MagicDraw中文问题
- 交换网络组建实验报告
- 转:用友华表Cell报表面面观
- ie11浏览器For win7 x86 官方下载 V9600 官方版
- MySQL-8.0.11-winx64.zip安装教程(Win10操作系统)
- 50个Windows自带软件的免费替代品
- Mac/Macbook Apple ID 无法登录:验证失败,发生未知错误
- UU跑腿前端中台方案
- 风行电视账号服务器链接失败,腾讯对战平台连接版本服务器失败的解决方法
- 完美解决C#中拖动splitContainer分割线时显示虚线问题