Android 自定义状态栏实例代码

所属分类: 软件编程 / Android 阅读数: 18
收藏 0 赞 0 分享

一、目标:Android5.0以上

二、步骤

1、在res-values-colors.xml下新建一个RGB颜色

  <?xml version="1.0" encoding="utf-8"?> 
  <resources> 
    <color name="colorPrimary">#3F51B5</color> 
    <color name="colorPrimaryDark">#303F9F</color> 
    <color name="colorAccent">#FF4081</color> 
    <color name="theRed">#ff6a69</color> 
  </resources> 

2、新建一个布局,名为actionbarlayout.xml,在后边重写布局时用于添加

<?xml version="1.0" encoding="utf-8"?> 
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
      android:id="@+id/actionBarId" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      /> 
  </LinearLayout> 

3、重写LineaLayout布局,放置步骤二新建的布局在顶部,用作背景颜色的容器

  public class ActionBarLayOut extends LinearLayout { 
    public ActionBarLayOut(Context context, AttributeSet attrs){ 
      super(context,attrs); 
      LayoutInflater.from(context).inflate(R.layout.actionbarlayout,this); 
    } 
  }

3、在主布局里调用这个重写后的线性布局

<?xml version="1.0" encoding="utf-8"?> 
  <com.example.test.ActionBarLayOut xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context="com.example.test.MainActivity"> 
    <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/theRed" 
      android:text="Hello World!" /> 
  </com.example.test.ActionBarLayOut> 

 4、在main活动中进行相应设置

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      TextView textView = (TextView)findViewById(R.id.actionBarId); 
      int color = getResources().getColor(R.color.theRed); 
      setActionBarColor(textView,color); 
    } 
    protected void setActionBarColor(TextView textView, int ActionBarColor){ 
      //----------------------------------隐藏标题栏---------------------------------------------- 
      if (getSupportActionBar()!=null) { 
        getSupportActionBar().hide(); 
      } 
      //------------------------------------------------------------------------------------------ 
      //----------------------------------将状态栏设置为透明-------------------------------------- 
      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
        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); 
        window.setStatusBarColor(Color.TRANSPARENT); 
        window.setNavigationBarColor(Color.TRANSPARENT); 
      } 
      //------------------------------------------------------------------------------------------ 
      /** 
       * 首先获取状态栏的高度statusBarHeight1,然后在状态栏的位置放一个空的TextView, 
       * 高度设置为statusBarHeight1,然后将TextView的背景颜色进行设置,这样就可以变相 
       * 的给状态栏设置颜色 
       */ 
      int statusBarHeight1 = -1; 
      //获取status_bar_height资源的ID 
      int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); 
      if (resourceId > 0) { 
        //根据资源ID获取响应的尺寸值 
        statusBarHeight1 = getResources().getDimensionPixelSize(resourceId); 
      } 
      textView.setHeight(statusBarHeight1); 
      textView.setBackgroundColor(ActionBarColor); 
    } 
  } 

以上所述是小编给大家介绍的Android 自定义状态栏实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多