当前位置: 移动技术网 > IT编程>移动开发>Android > Android 自定义状态栏实例代码

Android 自定义状态栏实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

上海九龙男子医院,三嫁李郎,刘根山

一、目标: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 自定义状态栏实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网