先上效果圖
總共有三種方法,下面一一介紹:
第一種
佈局中頂層容器配置:
android:clipToPadding="true"
android:fitsSystemWindows="true"
結果如下
在Activity對應的style裏面設置:
<item name="windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
總共是這樣
第二種
在Activity代碼中添加:
private void initWindows() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.BLACK);
}
}
然後在setContentView方法前調用即可
如下圖
第三種
調用第三方jar包
在gradle中配置:
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
如下圖
在Activity裏添加:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//透明狀態欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明導航欄
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
SystemBarTintManager tintManager = new SystemBarTintManager(this);
// 激活狀態欄
tintManager.setStatusBarTintEnabled(true);
// enable navigation bar tint 激活導航欄
tintManager.setNavigationBarTintEnabled(true);
//設置系統欄設置顏色
//tintManager.setTintColor(R.color.red);
//給狀態欄設置顏色
tintManager.setStatusBarTintColor(Color.GREEN);
//Apply the specified drawable or color resource to the system navigation bar.
//給導航欄設置資源
tintManager.setNavigationBarTintResource(R.mipmap.ic_launcher);
}
如圖
註釋很明確了,我也不多說。
最後我還要添一個,如果想讓狀態欄背景欄顏色和該頁面頂端顏色一樣,就用我下面這個。配置和第一種都一樣,自己回上去去看。
然後在Activity裏添加代碼
代碼如下:
new Thread(){
@Override
public void run() {
super.run();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
int pixel = bitmap.getPixel(1, 1);//獲取顏色
int redValue = Color.red(pixel);
int blueValue = Color.blue(pixel);
int greenValue = Color.green(pixel);
Message message = new Message();
message.what = 123;
message.obj = redValue;
message.arg1 = greenValue;
message.arg2 = blueValue;
handler.sendMessage(message);
Log.d("MainActivity.this", "redValue" + redValue + "blueValue" + blueValue + "greenValue" +
greenValue + "height" + bitmap.getHeight() + "width" + bitmap.getWidth());
}
}.start();
和
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)
{
case 123:
activity_main.setBackgroundColor(Color.argb(255, (int)msg.obj, msg.arg1, msg.arg2));
}
}
};
什麼意思呢?就是獲取該頁面頂端圖片座標(1,1)的像素,然後傳出去,再設置狀態欄顏色,很簡單了。
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);這裏的R.drawable.pic就是我們需要分析的圖片,每個人都不一樣,自己填進去即可。
最後給出資源:http://download.csdn.net/detail/wanxuedong/9890210