日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android DecorView的使用

發布時間:2024/4/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android DecorView的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.



今天,簡單講講android 中的 

DecorView的使用。


getWindow().getDecorView()的方法可以獲取到decorView,decorView是什么呢?
decorView是window中的最頂層view,可以從window中獲取到decorView,然后decorView有個getWindowVisibleDisplayFrame方法可以獲取到程序顯示的區域,包括標題欄,但不包括狀態欄。

Rect rect = new Rect(); /* * getWindow().getDecorView()得到的View是Window中的最頂層View,可以從Window中獲取到該View, * 然后該View有個getWindowVisibleDisplayFrame()方法可以獲取到程序顯示的區域, * 包括標題欄,但不包括狀態欄。 */ getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); 1.獲取狀態欄高度: 根據上面所述,我們可以通過rect對象得到手機狀態欄的高度 int statusBarHeight = rect.top; 2.獲取標題欄高度: getWindow().findViewById(Window.ID_ANDROID_CONTENT); 該方法獲取到的View是程序不包括標題欄的部分,這樣我們就可以計算出標題欄的高度了。 int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); //statusBarHeight是上面所求的狀態欄的高度 int titleBarHeight = contentTop - statusBarHeight


接下來舉一個具體的例子:

先來看看實現的效果


實現的大致思路

  • 首先需要明白什么是DecorView,他是android中界面的根布局。其實android的activity界面整個就是一個控件樹,DecorView是根節點,DecorView的孩子節點就是一個LinearLayout,這個LinearLayout的孩子系節點就包括狀態欄 + 和我們自己寫的布局
  • DecorView是FramLayout的子類(可以從源碼中看到)
  • 既然DecorView是根節點,而且還是FrameLayout,所以我們可以把我們自己的布局 添加到DecorView 或者 從DecorView移除,這樣就模擬出了一個Dialog的效果~~ ,當然這個Dialog的樣式,動畫就可以自己想怎么寫就怎么寫了撒
  • 通過activity.getWindow().getDecorView()可以獲得DecorView

  • [下面大量 代碼 ]

    第一個對話框的實現

    public class TipsDialog {private Activity activity;private View rootView;private TextView confirmTextView;private TextView cancelTextView;private TextView contentTextView;private boolean isShowing;public TipsDialog(Activity activity) {this.activity = activity;isShowing = false;rootView = LayoutInflater.from(activity).inflate(R.layout.view_tips_dialog,null);confirmTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_confirm);cancelTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_cancel);contentTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_content);}public void show(){if(activity == null){return;}if(isShowing){return;}ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);params.gravity = Gravity.CENTER;rootView.setLayoutParams(params);decorView.addView(rootView);rootView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dismiss();}});RotateAnimation rotateAnimation = new RotateAnimation(0,720f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);rotateAnimation.setDuration(2000);contentTextView.startAnimation(rotateAnimation);isShowing = true;}public void dismiss(){if(!isShowing){return;}isShowing = false;if(rootView.getParent() == null){return;}ViewGroup parent = (ViewGroup) rootView.getParent();parent.removeView(rootView);}public int getRandomColor(){Random random = new Random();return Color.argb(random.nextInt(200),random.nextInt(240),random.nextInt(240),random.nextInt(240));}public boolean isShowing() {return isShowing;} }


    其實就是show的時候將布局添加到DecorView上面去,dismiss的時候將布局從DecorView上面移除


    提示的實現(沒有處理完善~~ 僅僅就是說明哈DecorView)

    public class TopTipDialog {private Activity activity;private View rootView;private boolean isShowing;private static final int VIEW_HEIGHT = 64;//pxpublic TopTipDialog(Activity activity) {this.activity = activity;rootView = LayoutInflater.from(activity).inflate(R.layout.view_top_tip_dialog,null);}public void show(){if(isShowing){return;}ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, VIEW_HEIGHT);params.gravity = Gravity.TOP;params.setMargins(0,0,0,-VIEW_HEIGHT);rootView.setLayoutParams(params);TranslateAnimation translateAnimation = new TranslateAnimation(0,0,-VIEW_HEIGHT,0);translateAnimation.setDuration(1500);translateAnimation.setFillAfter(true);decorView.addView(rootView);rootView.startAnimation(translateAnimation);rootView.postDelayed(new Runnable() {@Overridepublic void run() {TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,-VIEW_HEIGHT);translateAnimation1.setDuration(1500);translateAnimation1.setFillAfter(true);rootView.startAnimation(translateAnimation1);}},3000);} }


    android DecorView的使用就講完了。


    就這么簡單。

    總結

    以上是生活随笔為你收集整理的android DecorView的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。