android透明状态栏
2015-12-02 13:11
302 查看
miui v7 android4.4.4效果 android 6.0 效果
基类Activity
使用
布局
颜色资源
注意,要设置没有标题栏,可以在activity中设置,也可以在xml中配置:
values文件夹中styles.xml
上面的方案中键盘弹出时可能导致布局上移,但从状态栏依旧能看到布局,采用下面方案可解决
package com.example.imageloader;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class SupportActivity extends FragmentActivity {
private View statusBar;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
statusBar=new View(this);
ViewGroup.LayoutParams param=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight());
statusBar.setLayoutParams(param);
setStatusBarColor(0);
}
}
/**
* 设置状态栏颜色
* @param color 0 黑色
*/
protected void setStatusBarColor(int color){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (color==0){
statusBar.setBackgroundColor(Color.BLACK);
}else {
statusBar.setBackgroundColor(getResources().getColor(color));
}
}
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
FrameLayout container=new FrameLayout(this);
//在用户布局外面嵌套一个FrameLayout,防止用户布局中根控件的margin失效
LayoutInflater.from(this).inflate(layoutResID,container,true);
setContentView(container);
}
@Override
public void setContentView(View view) {
super.setContentView(view);
adjustContentMargin(view);
((ViewGroup)getWindow().getDecorView()).addView(statusBar);
}
private void adjustContentMargin(View view){
ViewGroup.LayoutParams param=view.getLayoutParams();
ViewGroup.MarginLayoutParams margParam;
if (param instanceof ViewGroup.MarginLayoutParams){
margParam=(ViewGroup.MarginLayoutParams) param;
margParam.setMargins(margParam.leftMargin,margParam.topMargin+getStatusBarHeight(),margParam.rightMargin,margParam.bottomMargin);
}else{
margParam=new ViewGroup.MarginLayoutParams(param);
margParam.setMargins(0,getStatusBarHeight(),0,0);
}
view.setLayoutParams(margParam);
}
protected int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
基类Activity
package com.example.sss; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.annotation.LayoutRes; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; public class SupportActivity extends FragmentActivity { @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); // Translucent status bar window.setFlags( WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); setStatusBarColor(0); } } /** * 设置状态栏颜色 * @param color 0 黑色 */ protected void setStatusBarColor(int color){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (color==0){ getWindow().getDecorView().setBackgroundColor(Color.BLACK); }else { getWindow().getDecorView().setBackgroundColor(getResources().getColor(color)); } } } @Override public void setContentView(@LayoutRes int layoutResID) { FrameLayout container=new FrameLayout(this); //在用户布局外面嵌套一个FrameLayout,防止用户布局中根控件的margin失效 LayoutInflater.from(this).inflate(layoutResID,container,true); setContentView(container); } @Override public void setContentView(View view) { super.setContentView(view); adjustContentMargin(view); } private void adjustContentMargin(View view){ ViewGroup.LayoutParams param=view.getLayoutParams(); ViewGroup.MarginLayoutParams margParam; if (param instanceof ViewGroup.MarginLayoutParams){ margParam=(ViewGroup.MarginLayoutParams) param; margParam.setMargins(margParam.leftMargin,margParam.topMargin+getStatusBarHeight(),margParam.rightMargin,margParam.bottomMargin); }else{ margParam=new ViewGroup.MarginLayoutParams(param); margParam.setMargins(0,getStatusBarHeight(),0,0); } view.setLayoutParams(margParam); } protected int getStatusBarHeight() { int result = 0; int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = getResources().getDimensionPixelSize(resourceId); } return result; } }
使用
package com.example.sss; import android.annotation.SuppressLint; import android.os.Bundle; public class MainActivity extends SupportActivity { @SuppressLint("ResourceAsColor") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setStatusBarColor(R.color.colorPrimary); } }
布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" android:background="#fff" > <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/colorPrimary" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="标题" android:textSize="20sp" android:textColor="#fff" android:layout_centerInParent="true"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" android:layout_centerVertical="true" android:textColor="#fff" /> </RelativeLayout> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:gravity="center" android:text="hello" /> </LinearLayout>
颜色资源
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#18b4ed</color> <color name="colorPrimaryDark">#18b4ed</color> <color name="colorAccent">#FF4081</color> <color name="colorRed">#ff003b</color> </resources>
注意,要设置没有标题栏,可以在activity中设置,也可以在xml中配置:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sss" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
values文件夹中styles.xml
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> </resources>
上面的方案中键盘弹出时可能导致布局上移,但从状态栏依旧能看到布局,采用下面方案可解决
package com.example.imageloader;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class SupportActivity extends FragmentActivity {
private View statusBar;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// Translucent status bar
window.setFlags(
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
statusBar=new View(this);
ViewGroup.LayoutParams param=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight());
statusBar.setLayoutParams(param);
setStatusBarColor(0);
}
}
/**
* 设置状态栏颜色
* @param color 0 黑色
*/
protected void setStatusBarColor(int color){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (color==0){
statusBar.setBackgroundColor(Color.BLACK);
}else {
statusBar.setBackgroundColor(getResources().getColor(color));
}
}
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
FrameLayout container=new FrameLayout(this);
//在用户布局外面嵌套一个FrameLayout,防止用户布局中根控件的margin失效
LayoutInflater.from(this).inflate(layoutResID,container,true);
setContentView(container);
}
@Override
public void setContentView(View view) {
super.setContentView(view);
adjustContentMargin(view);
((ViewGroup)getWindow().getDecorView()).addView(statusBar);
}
private void adjustContentMargin(View view){
ViewGroup.LayoutParams param=view.getLayoutParams();
ViewGroup.MarginLayoutParams margParam;
if (param instanceof ViewGroup.MarginLayoutParams){
margParam=(ViewGroup.MarginLayoutParams) param;
margParam.setMargins(margParam.leftMargin,margParam.topMargin+getStatusBarHeight(),margParam.rightMargin,margParam.bottomMargin);
}else{
margParam=new ViewGroup.MarginLayoutParams(param);
margParam.setMargins(0,getStatusBarHeight(),0,0);
}
view.setLayoutParams(margParam);
}
protected int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories