吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5375|回复: 2
收起左侧

[会员申请] 申请会员ID:898

[复制链接]
吾爱游客  发表于 2019-8-14 15:46
1、申 请 I D :898
2、个人邮箱:haomin567@163.com
3、原创技术文章:   
实现Android沉浸式状态栏 ,状态栏改色。
image.png

实现半透明状态栏
       因为本例使用了NavigationView,所以布局代码稍多,当然如果你不需要,可以自己进行筛减。
       注意引入相关依赖:
Java代码
  • compile 'com.android.support:appcompat-v7:22.2.1'
  • compile 'com.android.support:support-v4:22.2.1'
  • compile 'com.android.support:design:22.2.0'

(一)colors.xml 和 styles.xml
       首先我们定义几个颜色:
       res/values/color.xml
XML/HTML代码
  • <?xml version="1.0" encoding="utf-8"?>
  • <resources>
  •     <color name="primary">#FF03A9F4</color>
  •     <color name="primary_dark">#FF0288D1</color>
  •     <color name="status_bar_color">@color/primary_dark</color>
  • </resources>

       下面定义几个styles.xml
       注意文件夹的路径:
       values/styles.xml
XML/HTML代码
  • <resources>
  •     <style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  •         <!-- Customize your theme here. -->
  •         <item name="colorPrimary">@color/primary</item>
  •         <item name="colorPrimaryDark">@color/primary_dark</item>
  •         <item name="colorAccent">#FF4081</item>
  •     </style>
  •     <!-- Base application theme. -->
  •     <style name="AppTheme" parent="@style/BaseAppTheme">
  •     </style>
  • </resources>

       values-v19
XML/HTML代码
  • <resources>
  •     <style name="AppTheme" parent="@style/BaseAppTheme">
  •         <item name="android:windowTranslucentStatus">true</item>
  •     </style>
  • </resources>

       ok,这个没撒说的。注意我们的主题是基于NoActionBar的,android:windowTranslucentStatus这个属性是v19开始引入的。
      (二)布局文件
       activity_main.xml
XML/HTML代码
  • <android.support.v4.widget.DrawerLayout
  •     xmlns:android="http://schemas.android.com/apk/res/android"
  •     xmlns:app="http://schemas.android.com/apk/res-auto"
  •     xmlns:tools="http://schemas.android.com/tools"
  •     android:layout_width="match_parent"
  •     android:layout_height="match_parent"
  •     >
  •     <LinearLayout
  •         android:id="@+id/id_main_content"
  •         android:layout_width="match_parent"
  •         android:layout_height="match_parent"
  •         android:orientation="vertical">
  •         <android.support.v7.widget.Toolbar
  •             android:id="@+id/id_toolbar"
  •             android:layout_width="match_parent"
  •             android:layout_height="wrap_content"
  •             android:background="?attr/colorPrimary"
  •             android:fitsSystemWindows="true"
  •             app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
  •         <TextView
  •             android:id="@+id/id_tv_content"
  •             android:layout_width="match_parent"
  •             android:layout_height="0dp"
  •             android:layout_weight="1"
  •             android:gravity="center"
  •             android:text="HelloWorld"
  •             android:textSize="30sp"/>
  •     </LinearLayout>
  •     <android.support.design.widget.NavigationView
  •         android:id="@+id/id_nv_menu"
  •         android:layout_width="match_parent"
  •         android:layout_height="match_parent"
  •         android:layout_gravity="start"
  •         android:fitsSystemWindows="true"
  •         app:headerLayout="@layout/header_just_username"
  •         app:menu="@menu/menu_drawer"
  •         />
  • </android.support.v4.widget.DrawerLayout>

       DrawerLayout内部一个LinearLayout作为内容区域,一个NavigationView作为菜单。
       注意下Toolbar的高度设置为wrap_content。
       然后我们的NavigationView中又依赖一个布局文件和一个menu的文件。
       header_just_username.xml
XML/HTML代码
  • <?xml version="1.0" encoding="utf-8"?>
  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  •                 android:layout_width="match_parent"
  •                 android:layout_height="192dp"
  •                 android:background="?attr/colorPrimaryDark"
  •                 android:orientation="vertical"
  •                 android:padding="16dp"
  •                 android:fitsSystemWindows="true"
  •                 android:theme="@style/ThemeOverlay.AppCompat.Dark">
  •     <TextView
  •         android:id="@+id/id_link"
  •         android:layout_width="wrap_content"
  •         android:layout_height="wrap_content"
  •         android:layout_alignParentBottom="true"
  •         android:layout_marginBottom="16dp"
  •         android:text="http://blog.csdn.net/lmj623565791"/>
  •     <TextView
  •         android:id="@+id/id_username"
  •         android:layout_width="wrap_content"
  •         android:layout_height="wrap_content"
  •         android:layout_above="@id/id_link"
  •         android:text="Zhang Hongyang"/>
  •     <ImageView
  •         android:layout_width="72dp"
  •         android:layout_height="72dp"
  •         android:layout_above="@id/id_username"
  •         android:layout_marginBottom="16dp"
  •         android:src="@mipmap/ic_launcher"/>
  • </RelativeLayout>

       menu的文件就不贴了,更加详细的可以去参考Android 自己实现 NavigationView [Design Support Library(1)]。
       大体看完布局文件以后,有几个点要特别注意:
       &#8226; ToolBar高度设置为wrap_content
       &#8226; ToolBar添加属性android:fitsSystemWindows="true"
       &#8226; header_just_username.xml的跟布局RelativeLayout,添加属性android:fitsSystemWindows="true"
       android:fitsSystemWindows这个属性,主要是通过调整当前设置这个属性的view的padding去为我们的status_bar留下空间。
       根据上面的解释,如果你不写,那么状态栏和Toolbar就会有挤一块的感觉了。

       ok,最后看下代码。
       (三)Activity的代码
Java代码
  • package com.zhy.colorfulstatusbar;
  • import android.os.Bundle;
  • import android.support.v7.app.AppCompatActivity;
  • import android.support.v7.widget.Toolbar;
  • public class MainActivity extends AppCompatActivity
  • {
  •     @Override
  •     protected void onCreate(Bundle savedInstanceState)
  •     {
  •         super.onCreate(savedInstanceState);
  •         setContentView(R.layout.activity_main);
  •         Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
  •         setSupportActionBar(toolbar);
  •         //StatusBarCompat.compat(this, getResources().getColor(R.color.status_bar_color));
  •         //StatusBarCompat.compat(this);
  •     }
  • }


d1

d1

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2019-8-14 16:51
抱歉,未能达到申请要求,申请不通过,可以关注论坛官方微信(吾爱破解论坛),等待开放注册通知。
saomu007 发表于 2019-8-14 18:40
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 01:24

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表