荣耀彩票代理

  • 热门专题

分别用ToolBar和自定义导航栏实现沉浸式状态栏

作者:熊,我-  发布日期:2016-12-26 20:23:29
  • 一、ToolBar

    1、在build.gradle中添加依赖,例如:

    compile 'com.android.support:appcompat-v7:23.4.0'

    2、去掉应用的ActionBar。可以是修改主题theme为“NoActionBar”,例如:

    <style name='AppTheme' parent='Theme.AppCompat.Light.NoActionBar'>
    

    或者不修改主题为'NoActionBar',而在主题的style下,添加:

        <item name='windowNoTitle'>true</item>
        <item name='windowActionBar'>false</item>

    荣耀彩票代理DIERGESHUXINGDAIBIAOSHIFOUYONGActionBarDAITITitleBar。

    QISHI,GANGXUEDESHIHOU,GANJUEHENNAMEN,ZENMEYOUDUOLEGETitleBar?HOULAICHALEHENJIUCAIFAXIAN,3.0YIQIAN,ZHUANGTAILANXIAMIANDESHIBIAOTILAN(ZHINENGXIANSHIBIAOTIDENGSHAOLIANGXINXI),3.0YIHOUJIUBIANCHENGLEYINGYONGLAN,YEJIUSHIActionBar。

    荣耀彩票代理LINGWAI,WOCESHIDESHIHOU,activitySHIJICHENGYUAppCompatActivity,ZHUTISHIAppCompatLEIXINGDE。ZHEIZHONGQINGKUANGXIA,BIXUYAOXIANGSHANGMIANNEIYANGXIECAIYOUXIAOGUO,SHAOXIEHUOZHIBUTONGDEHUA,YAOMEMEIXIAOGUO,YAOMEBAOCUO。

    ZUIHOU,SHANGMIANLIANGGESHUXINGDESHUOMINGKEZAIandroid.R.attrZHEIGELEIZHONGCHAKAN。

    3、在xml中为ToolBar添加属性

        android:fitsSystemWindows='true'
        android:minHeight='?attr/actionBarSize'

    fitsSystemWindowsSHIToolBarSHIXIANCHENJINSHIZHUANGTAILANDEGUANJIAN,QIDAGAIQINGKUANGSHI,RUGUOSHEWEItrue,JIUHUIDIAOZHENGZHEIGEviewQULIUYIXIEKONGJIANJIXITONGCHUANGKOU,RUGUOBUSHEZHIHUOSHEWEIfalse,ToolBarJIUHUIHEZHUANGTAILANZHONGDIEZAIYIQI。

    荣耀彩票代理ERDIERGESHUXINGZHONG,TADEZHIQUANXIESHI'?android:attr/actionBarSize',QIYISISHIYINYONGDANGQIANZHUTIZHONGDEactionBarSizeZHEIGESHUXING。GENGDUOXIANGGUANSHUOMINGKECHAKANGUANFANGWENDANGZHONGAccessing ResourcesDEBUFEN。

    SHANGMIANLIANGGESHUXINGKEZAIandroid.view.ViewZHEIGELEIZHONGCHAKAN。

    4、在java中添加判断sdk版本的代码并在用户的系统是4.4及以上时设置状态栏为透明

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }

    荣耀彩票代理WULUNSHIToolBar,HAISHIZIDINGYIDAOHANGLAN,ZHEIGECAOZUODOUSHISHIXIANCHENJINSHIZHUANGTAILANDEGUANJIAN。

    荣耀彩票代理YINWEISHEZHIZHUANGTAILANWEITOUMINGDEZHEIGESHUXING,YAO4.4YISHANGCAINENGSHIYONG,SUOYI4.4YIXIADEXITONGSHIBUNENGGOUSHIXIANCHENJINSHIZHUANGTAILANDE。ERZAI4.4DAO5.0DEXITONGZHONG,ZHUANGTAILANSHIQUANTOUMINGDE,YEJIUSHITADEYANSEHUIGENNIDEToolBarHEZIDINGYIDAOHANGLANDEYANSEYIYANG。ERZAI5.0YISHANGDEXITONGZHONG,ZESHIBANTOUMINGDE,YEJIUKANQILAIHUIBIJIAOSHENAN。

    ERWOZAI6.0DEXITONGSHANGCESHISHI,FAXIANZHEIYIBUMEISHEZHIHESHEZHILEDE,CONGXIAOGUOSHANGKAN,QUBIEJIUSHIMEISHEZHISHIZHUANGTAILANYANSEQIANYIDIAN,ERQIEToolBarDEpadding topWEI0,ERSHEZHILEDEYANSEJIUSHENYIDIAN,padding topWEIZHUANGTAILANDEGAODU。JUTIYOUSHENMEYINGXIANG,HAIBUQINGCHU。DANZHEIHUIRANGZIDINGYIDAOHANGDEWAIGUANBIANXING,TAHUIZENGJIAZHUANGTAILANDEGAODU,DANYOUMEIYOURANGZHEIBUFENYUZHUANGTAILANZHONGDIE,JIUDAOZHIXIAOGUOBIANXING。

    5、最后在java中添加

    setSupportActionBar(mToolbar);

    ToolBarDEBUJUDAIMA:

    <?xml version='1.0' encoding='utf-8'?>
    <android.support.v7.widget.Toolbar
        xmlns:android='http://schemas.android.com/apk/res/android'
        xmlns:app='http://schemas.android.com/apk/res-auto'
        android:id='@+id/tool_bar'
        android:layout_width='match_parent'
        android:layout_height='wrap_content'
        android:background='@color/colorPrimary'
        android:elevation='4dp'
        android:fitsSystemWindows='true'
        android:minHeight='?android:attr/actionBarSize'
        app:title='ToolBar'
        app:subtitle='toolbar'/>

     效果图(Android 6.0):

    二、自定义导航栏TopBar

    1、设置窗口为无标题,上面第2步中的两个方法都可以实现,或者是在java中添加如下代码:

            requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    ZHUYIZAITIANJIAZHEIJUDAIMASHI,QUEBAOSHIZAIJIAZAIBUJUNEIRONGZHIQIAN,YEJIUSHIonCreateDEsetContentViewZHIQIAN。

    LINGWAI,WOFAXIANRUGUOGAIactivitySHIJICHENGAppCompatActivityDEHUA,ZHIXIESHANGMIANDEZHEIJUDAIMASHIMEIYOUBIANHUADE,XIANSHIDEHAISHIActionBar。DANRUGUOSHIJICHENGFragmentActivityDEHUA,JIUYOUXIAOGUO,YEJIUSHUOSHANGMIANDI2BUZHONGDEDIERGEFANGFA,ZHITIANJIAQIZHONGRENYIYIGESHUXINGDOUSHIKEYIDE。ZHIYUSHISHENMEYUANYIN,WOHAIMEINONGQINGCHU。

    2、同上面第4步,判断系统版本并按需设置状态栏为透明

    3、获取状态栏的高度

        protected int getStatusHeight() {
            try {
                Class<?> c = Class.forName('com.android.internal.R$dimen'); // 获得与字符串对应的Class对象
                Object object = c.newInstance(); // 创建这个Class的实例对象
                Field field = c.getField('status_bar_height'); // 拿到字符串对应的变量
                int x = Integer.parseInt(field.get(object).toString()); // 通过这个实例对象拿到这个变量的值,再转换类型,最后转为整型,变为一个资源id
                return getResources().getDimensionPixelSize(x);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;
        }

    ZHEIBUFENDAIMASHILIYONGJavaDEFANSHEJIZHILAISHIXIANDE,YINWEIZHEIGEinternalBAOMORENHUIBEIsdk/platforms/android-versionZHONGDEandroid.jarJIYICHUDIAO,SUOYIWUFAZHIJIEDIAOYONGHUOCHAKANZHEIGEBAOZHONGDELEI。RUGUOYAOSHIYONGDEHUA,KEYIJIEZHUZHEIGEKAIYUANXIANGMUhttp://github.com/anggrayudi/android-hidden-api。

    4、获取自定义TopBar的高度并修改布局参数

        protected void setStatusBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.top_bar);
                final int statusHeight = getStatusHeight();
                viewGroup.post(new Runnable() {
                    @Override
                    public void run() {
                        int topBarHeight = viewGroup.getHeight();
                        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewGroup.getLayoutParams();
                        layoutParams.height = statusHeight + topBarHeight;
                        viewGroup.setLayoutParams(layoutParams);
                    }
                });
            }
        }

    YINWEIZAIincludeZHEIGETopBarDEBUJUWENJIANZHONG,QIFUBUJUSHILinearLayout,ERTopBarDEFUBUJUSHIRelativeLayout,SUOYIZHEILIXIANYAOZHUANCHENGViewGroup,DENGgetLayoutParamsSHI,ZAIZHUANCHENGLinearLayout.LayoutParams。

    TopBarDEBUJU:

    <?xml version='1.0' encoding='utf-8'?>
    <RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android'
        android:id='@+id/top_bar'
        android:layout_width='match_parent'
        android:layout_height='49dp'
        android:background='@color/colorPrimary'
        android:gravity='bottom'>
        
        <RelativeLayout
            android:layout_width='match_parent'
            android:layout_height='49dp'>
    
            <TextView
                android:layout_width='wrap_content'
                android:layout_height='wrap_content'
                android:layout_centerInParent='true'
                android:text='@string/app_name'
                android:textSize='24sp'
                android:textColor='#ffffff'/>
    
        </RelativeLayout>
    
    </RelativeLayout>

    YINWEIZHEIGEBUJUDEGAODUHUIZAIDAIMAZHONGDONGTAIDIXIUGAI,JI49dpJIASHANGZHUANGTAILANDEGAODU,SUOYIZHIYOUYIGECENGJIDEJIEGOUDEHUA,NEIDAOHANGLANDENEIRONGJIUHUIWANGSHANGPIAN。SUOYIYAOQIANTAODUOYICENGLAIWEICHIDAOHANGLANDEGAODU,TONGSHIZAIZUIWAICENGDEBUJUZHONG,TIANJIAandroid:gravity='bottom'ZHEIGESHUXINGLAIBAOZHENGDAOHANGLANBUWANGSHANGPIAN。

    效果图(Android 6.0):

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规