嘗試各種MD中
這里只實(shí)現(xiàn)圖片延伸
- application里面主題是
android:theme="@style/AppTheme"
狀態(tài)欄背景透明
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
將要延伸的布局設(shè)置屬性
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(R.id.rootview));
rootView.setFitsSystemWindows(true);
rootView.setClipToPadding(true);
rootview是最上面的子布局,我的rootview是AppBarLayout,按需求切換
Toolbar未設(shè)置透明色,所以是藍(lán)色.
AppBarLayout=Toolbar+TabLayout,AppBarLayout的背景是下面的圖片
這樣子整個(gè)布局就上去了,發(fā)現(xiàn)狀態(tài)欄效果是透明的一層貼圖,整個(gè)下面布局頂?shù)搅似聊簧线吙?我們要的效果是圖片延伸上去,控件照舊.所以就在原布局里面的最上面加一個(gè)和狀態(tài)欄一樣大小的透明view,然后設(shè)置背景圖.
添加view后的代碼(oncreate部分完整代碼)###
//狀態(tài)欄透明
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// 生成一個(gè)狀態(tài)欄大小的矩形
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
// 繪制一個(gè)和狀態(tài)欄一樣高的矩形
View statusView = new View(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight);
statusView.setLayoutParams(params);
statusView.setBackgroundColor(Color.TRANSPARENT);
// 添加 statusView 到布局中
ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(R.id.rootview));
rootView.addView(statusView, 0);// addView(ViewGroup view, index);
rootView.setFitsSystemWindows(true);
rootView.setClipToPadding(true);
...
// 順便把Toolbar的顏色設(shè)置為透明
...