很简单,在BaseActivity添加代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayout());
//判断当前SDK版本号,如果是4.4以上,就是支持沉浸式状态栏的
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
TitleBuilder titleBuilder = new TitleBuilder(this);
titleBuilder.setTitleText(res.getString(R.string.app_name));
}
TitleBuilder.java
public TitleBuilder(final Activity context) {
View root = TypefaceUtil.getRootView(context);
isLinearLayout = root instanceof LinearLayout;
if (isLinearLayout) {
LinearLayout rootView = (LinearLayout) root;
// rootView.setClipToPadding(true);
// rootView.setFitsSystemWindows(true);
// rootView.setBackgroundColor(context.getResources().getColor(R.color.subject_bg));
view = LayoutInflater.from(context).inflate(R.layout.top_toolbar, null);
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ConstantValue.TitleHeight));
rootView.addView(view, 0);
}
}
getRootView方法:
/**
* 从Activity 获取 rootView 根节点
* @param context
* @return 当前activity布局的根节点
*/
public static View getRootView(Activity context)
{
return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);
}
top_toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_toolbar"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/y30"
android:background="@color/subject_bg"
android:gravity="center_vertical"
android:paddingTop="@dimen/y10">
<LinearLayout
android:id="@+id/rl_toolbar_left"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingLeft="@dimen/x10"
android:gravity="left|center_vertical">
<ImageView
android:id="@+id/iv_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/common_back"/>
<TextView
android:id="@+id/tv_toolbar_left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#fff"
android:textSize="18sp"
android:visibility="gone"
android:gravity="center_vertical"
android:layout_marginLeft="@dimen/x10"/>
</LinearLayout>
<RelativeLayout
android:id="@+id/rl_toolbar_center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/tv_toolbar_center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="18sp"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_toolbar_right"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingRight="@dimen/x10">
<ImageView
android:id="@+id/iv_toolbar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:visibility="gone"
android:src="@mipmap/common_back"/>
<TextView
android:id="@+id/tv_toolbar_right"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textColor="#fff"
android:textSize="18sp"
android:gravity="center"
android:layout_alignParentRight="true"
android:layout_toLeftOf="@+id/iv_toolbar_right"/>
</RelativeLayout>
</LinearLayout>
这里根view一定要有个android:paddingTop=”@dimen/y10”属性,用来是标题栏看起来不会渗进状态栏里边。
注意到,TitleBuilder注释掉这两句话:
rootView.setClipToPadding(true);
rootView.setFitsSystemWindows(true);
大概意思就是在标题栏上方留出状态栏位置,但是光有这个的话状态栏的颜色是透明的,一定要和设置Activity的rootView背景色搭配使用。但是这样整个页面的背景色都变成主题色了,还得辛辛苦苦的将View的背景设置一番。
rootView.setBackgroundColor(context.getResources().getColor(R.color.subject_bg));
推荐使用paddingTop方式,简单易上手。