
状态栏和虚拟导航键 4.4上半透明,5.0以上可以全透明
先上效果
4.4 半透明效果
5.0及以上 全透明效果
上代码
MainActivity代码
public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 隐藏标题栏 supportRequestwindowFeature(Window.FEATURE_NO_Title); VIEw root = LayoutInflater.from(this).inflate(R.layout.activity_main,null); // 或者 在界面的根层加入 androID:fitsSystemwindows=”true” 这个属性,这样就可以让内容界面从 状态栏 下方开始。 VIEwCompat.setFitsSystemwindows(root,true); setContentVIEw(root); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LolliPOP) { // AndroID 5.0 以上 全透明 Window window = getwindow(); 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); // 状态栏(以上几行代码必须,参考setStatusbarcolor|setNavigationbarcolor方法源码) window.setStatusbarcolor(color.transparent); // 虚拟导航键 window.setNavigationbarcolor(color.transparent); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // AndroID 4.4 以上 半透明 Window window = getwindow(); // 状态栏 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // 虚拟导航键 window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } }}activity_main.xml代码:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/activity_main" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@color/colorPrimary" > <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Hello World!" /></relativeLayout>
5.0以上的几行代码不是很懂,从源码看是需要添加的,以后找到这几个方法是做什么用的再回来注明
setStatusbarcolor源码
/** * Sets the color of the status bar to {@code color}. * * For this to take effect,* the window must be drawing the system bar backgrounds with * {@link androID.vIEw.WindowManager.LayoutParams#FLAG_DRAWS_SYstem_bar_BACKGROUNDS} and * {@link androID.vIEw.WindowManager.LayoutParams#FLAG_TRANSLUCENT_STATUS} must not be set. * * If {@code color} is not opaque,consIDer setting * {@link androID.vIEw.VIEw#SYstem_UI_FLAG_LAYOUT_Stable} and * {@link androID.vIEw.VIEw#SYstem_UI_FLAG_LAYOUT_FulLSCREEN}. * <p> * The Transitionname for the vIEw background will be "androID:status:background". * </p> */ public abstract voID setStatusbarcolor(@colorInt int color);setNavigationbarcolor源码方法
/** * Sets the color of the navigation bar to {@param color}. * * For this to take effect,* the window must be drawing the system bar backgrounds with * {@link androID.vIEw.WindowManager.LayoutParams#FLAG_DRAWS_SYstem_bar_BACKGROUNDS} and * {@link androID.vIEw.WindowManager.LayoutParams#FLAG_TRANSLUCENT_NAVIGATION} must not be set. * * If {@param color} is not opaque,consIDer setting * {@link androID.vIEw.VIEw#SYstem_UI_FLAG_LAYOUT_Stable} and * {@link androID.vIEw.VIEw#SYstem_UI_FLAG_LAYOUT_HIDE_NAVIGATION}. * <p> * The Transitionname for the vIEw background will be "androID:navigation:background". * </p> */ public abstract voID setNavigationbarcolor(@colorInt int color);fitsSystemwindows属性需设置为true,否则布局会和状态栏重叠
如图:
两种方式:
方式一(xml文件根布局添加属性):
AndroID:fitsSystemwindows=”true”
方式二(代码中设置):
VIEwCompat.setFitsSystemwindows(rootVIEw,true);
其实还有第三种方式解决此问题,获取状态栏高度,在最上设置一个等高的VIEw
/** * 获取状态栏高度 * @return */ public int getStatusbarHeight() { int statusbarHeight = 0; int resourceID = getResources().getIDentifIEr("status_bar_height","dimen","androID"); if (resourceID > 0) { statusbarHeight = getResources().getDimensionPixelSize(resourceID); } return statusbarHeight; }源码地址:https://github.com/StormSunCC/MyCompatStatusBar
以上所述是小编给大家介绍的AndroID 状态栏虚拟导航键透明效果的实现方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 状态栏虚拟导航键透明效果的实现方法全部内容,希望文章能够帮你解决Android 状态栏虚拟导航键透明效果的实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)