為了我們的app看起來更漂亮,處理下通知欄是很有必要的寡键,下面是沉浸通知欄的一種實(shí)現(xiàn)方式。我們?cè)陧?xiàng)目中使用一些通用的方法時(shí)雪隧,要學(xué)會(huì)提煉出基礎(chǔ)類昌腰,供其他類來使用,減少代碼重復(fù)膀跌,增加程序健壯性和可讀性遭商。
-
Android沉浸通知欄,有一個(gè)開源庫SystemBarTint,效果如下:
使用方法:在Activity中添加相關(guān)代碼捅伤,Demo如下:
public class MatchActionBarActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_actionbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.statusbar_bg);//通知欄所需顏色
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
}
- 在APP中使用的時(shí)候劫流,我們不要在每個(gè)Activity中都重復(fù)寫這么一段代碼,而是要自己定義一個(gè)基礎(chǔ)的抽象Activity類丛忆,用于其他Activity繼承祠汇,減少代碼重復(fù)。這里給出我定義的一個(gè)基礎(chǔ)抽象Activity類BaseActivity.java,代碼如下:
package cn.studyou.parchment.activity;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import cn.studyou.parchment.R;
/**
* 基本功能:基礎(chǔ)Activity類
* 創(chuàng)建:王杰
* 創(chuàng)建時(shí)間:16/3/9
* 郵箱:w489657152@gmail.com
*/
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.green);//通知欄所需顏色
}
@TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
}
- 然后每個(gè)Activity中繼承BaseActivity即可熄诡,如下:
package cn.studyou.parchment.activity;
import android.os.Bundle;
import cn.studyou.parchment.R;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
- 最后一步可很,要在布局文件中添加如下代碼:
android:fitsSystemWindows="true"
android:clipToPadding="false"
- 添加后的布局文件代碼形式如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:fitsSystemWindows="true"
android:clipToPadding="false"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:textSize="@dimen/normal"/>
</RelativeLayout>