Android 5.0 Design 沉浸式状态栏

依赖官方提供的工程项目,在value21里面添加如下代码:

   <style name="AppBaseTheme" parent="@android:style/Theme.Material">

        <!-- 标题栏的颜色 (使用support包时似乎无效。) -->
        <item name="android:colorPrimary">#1470E4</item>
        <!-- 状态栏颜色-->
        <item name="android:colorPrimaryDark">#1448E4</item>
        <!-- 控件颜色 -->
        <item name="android:colorAccent">#1448E4</item>
    </style>

效果图:


沉浸式状态栏在低版本不兼容,更多的5.0 theme参照目录: @android:style/Theme.Materil..  

项目的依赖工程我做了一个5.0所有依赖的整合,下载地址:http://download.csdn.net/detail/anddroid_lanyan/8622901



新改:突然发现styles.xml麻烦有点,上面的方法在某个手机android5.1上面运行效果不理想,进过研究得出以下处理方式,再也不用values-v21

BasicActivity:

import java.lang.reflect.Field;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lidroid.xutils.ViewUtils;

/**
 * Created by LanYan on 2015/6/29.
 */
@SuppressLint("HandlerLeak")
public abstract class BasicActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		config();
		setupStatuBar(this);
		setContentView(getLayoutID());
		//Annotations view
		ViewUtils.inject(this);
		initView();
	}

	/*
	 * The annotation of the control of the UI update
	 * Method is protected ,child activity super..
	 * */
	protected void initView() {
	}

	/*
	 * Config application theme style,such as no title bar, or status bar, transparent, etc
	 * Method is protected,child activity super ..
	 **/
	protected void config() {

	}

	/*
	 * Build contentView id
	 * onCreate(Bundle saveInstanceState)>setContentView(R.layout.activity_main)
	 * R.layout.activity_main=getlayoutId();
	 * */
	public abstract int getLayoutID();


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		super.onResume();
	}
	protected  Handler mHandler =new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			decodeMessage(msg);
		}
	};

	protected void decodeMessage(Message msg) {

	}

	@SuppressLint("NewApi")
	protected void setupStatuBar(Activity activity) {  
		if (Build.VERSION.SDK_INT == 19) {  
			Window window = activity.getWindow();  
			int flags = window.getAttributes().flags;  
			if ((flags | WindowManager.LayoutParams.FLAG_FULLSCREEN) != flags) {  
				window.setFlags(  
						WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,  
						WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
				int height = getStatusbarHeight(activity);  
				View contentView = window  
						.findViewById(Window.ID_ANDROID_CONTENT);  
				contentView.setBackgroundColor(getResources().getColor(R.color.statuBar_color));  
				contentView.setPadding(0, height, 0, 0);  
			} else {  
				window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  
				View contentView = window  
						.findViewById(Window.ID_ANDROID_CONTENT);  
				contentView.setBackgroundColor(getResources().getColor(R.color.statuBar_color));  
				contentView.setPadding(0, 0, 0, 0);  
			}  
		}else if(Build.VERSION.SDK_INT>=21){
			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);
		}  
	}  
	protected int getStatusbarHeight(Context context) {  

		try {  
			Class<?> c = Class.forName("com.android.internal.R$dimen");  
			Object obj = c.newInstance();  
			Field field = c.getField("status_bar_height");  
			int x = Integer.parseInt(field.get(obj).toString());  
			int y = context.getResources().getDimensionPixelSize(x);  
			return y;  
		} catch (Exception e) {  
			e.printStackTrace();  
			return (int) (context.getResources().getDisplayMetrics().density*20+0.5);  
		}  
	}  
}

colors.xml

 <color name="statuBar_color">#FFFF0000</color>

个人不喜欢用ActionBar  ,所以通常用的主题在Application  配置:@android:style/Theme.Light.NoTitleBar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值