1. 程式人生 > >Android底部選單欄、Android沉浸式狀態列(頂部狀態列修改顏色)、自定義標題欄

Android底部選單欄、Android沉浸式狀態列(頂部狀態列修改顏色)、自定義標題欄

0、簡介:

沒有使用TabHost切換,而是變成FragmentActivity替換Fragment;沉浸式引用的git上面的jar包。

先看圖片

1、底部導航欄

核心程式碼

<span style="white-space:pre">	</span>/**
	 * 
	 * @param v
	 *            設定圖示字型狀態、改變顏色
	 */
	private void setBottom(View v) {
		//如果和上次點選的View不同並且不為空、設定上次的選中狀態為false;
		if (currentView != null && !currentView.equals(v)) {
			currentView.setSelected(false);
		}
		currentView = v;
		currentView.setSelected(true);
	}

2、Android沉浸式狀態列(頂部狀態列修改顏色)

核心程式碼:

在修改之前要在xml根部新增倆個引數

    android:clipToPadding="true"
    android:fitsSystemWindows="true"
解釋:

android:clipToPadding 定義佈局間是否有間距;

android:fitsSystemWindows="true" 你的佈局是否要考慮系統狀態列;如果不設定這兩個引數,可能你的佈局會擋住系統狀態列。

<span style="white-space:pre">		</span>// 設定當前Activity
		SettingHelper.getInstance().setCurrentActivity(MainActivity.this);
		// 設定狀態列為自定義顏色
		UIHelper.getInstance().setSystemBar();

<span style="white-space:pre">	</span>/**
	 * 設定系統狀態列
	 */
	public void setSystemBar() {
		// 修改沉浸式狀態列 要大於Android系統4.4 版本API19
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			setTranslucentStatus(true);
		}
		Activity activity = SettingHelper.getInstance().getCurrentActivity();
		// 獲取系統狀態列管理者
		SystemBarTintManager manager = new SystemBarTintManager(activity);
		// 是否修改
		manager.setStatusBarTintEnabled(true);
		// 修改的顏色
		manager.setStatusBarTintResource(R.color.app_overall);
	}

	@TargetApi(Build.VERSION_CODES.KITKAT)
	private void setTranslucentStatus(boolean is) {
		// 獲取當前Activity
		Activity activity = SettingHelper.getInstance().getCurrentActivity();
		Window window = activity.getWindow();
		WindowManager.LayoutParams params = window.getAttributes();
		final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
		if (is) {
			// 或之後賦予值
			params.flags |= bits;
		} else {
			// 與之後賦予值
			params.flags &= ~bits;
		}
		// 設定狀態引數
		window.setAttributes(params);
	}

3、標題欄

程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_head"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@color/app_overall" >

    <RelativeLayout
        android:id="@+id/layout_head_left_r"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp" >

        <ImageView
            android:id="@+id/layout_head_left_iv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/back_wihte"
            android:visibility="gone" />

        <TextView
            android:id="@+id/layout_head_left_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="@string/app_back"
            android:textColor="@color/app_white"
            android:textSize="15sp"
            android:visibility="gone" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/layout_head_centre_l"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/layout_head_centre_iv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:contentDescription="@string/app_name"
            android:src="@drawable/menu_about"
            android:visibility="gone" />

        <TextView
            android:id="@+id/layout_head_centre_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="3dp"
            android:text="@string/app_back"
            android:textColor="@color/app_white"
            android:textSize="15sp"
            android:visibility="gone" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/layout_head_right_r"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp" >

        <ImageView
            android:id="@+id/layout_head_right_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:contentDescription="@string/app_name"
            android:src="@drawable/menu_about"
            android:visibility="gone" />

        <TextView
            android:id="@+id/layout_head_right_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="@string/app_back"
            android:textColor="@color/app_white"
            android:textSize="15sp"
            android:visibility="gone" />
    </RelativeLayout>

</RelativeLayout>

4、下載地址