Android状态栏选型-颜色/沉浸式
方法1:values-v19下的styles.xml 设置windowTranslucentStatus为true
方法2:java中设置
注:以上方式支持一般的页面,但是像侧滑栏菜单这种事支持不了的!
关于侧滑栏菜单的沉侵式效果
要创建一个BaseActivity:
@TargetApi(19)
@Override
protected void onCreate(Bundle savedInstanceState) {
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
// // 全屏不隐藏状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// 设置状态栏透明
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
super.onCreate(savedInstanceState);
}
子Activity继承
@TargetApi(19)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_status = (TextView) findViewById(R.id.tv_status);
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
tv_status.setHeight(getStatusHeight(this));
tv_status.setVisibility(View.VISIBLE);
} else {
tv_status.setHeight(0);
tv_status.setVisibility(View.GONE);
}
}
//getStatusHeight方法为计算状态栏高度的方法:
public int getStatusHeight(Activity activity) {
int statusHeight = 0;
Rect localRect = new Rect();
activity.getWindow().getDecorView()
.getWindowVisibleDisplayFrame(localRect);
statusHeight = localRect.top;
if (0 == statusHeight) {
Class<?> localClass;
try {
localClass = Class.forName("com.android.internal.R$dimen");
Object localObject = localClass.newInstance();
int i5 = Integer.parseInt(localClass
.getField("status_bar_height").get(localObject)
.toString());
statusHeight = activity.getResources()
.getDimensionPixelSize(i5);
} catch (Exception e) {
e.printStackTrace();
}
}
return statusHeight;
}
布局Xml:
<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" >
<TextView
android:id="@+id/tv_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ccee" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#00ccee" >
<TextView
android:id="@+id/tv_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="菜单"
android:textColor="#FFFFFF"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="消息"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
注:tv_status控件就是用来设置为状态栏高度的
以上解决不了标题栏文字被提升的问题!下次会写出解决方法!
网友评论