溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android如何使用DecorView實現(xiàn)對話框功能

發(fā)布時間:2020-07-20 09:57:50 來源:億速云 閱讀:187 作者:小豬 欄目:移動開發(fā)

這篇文章主要講解了Android如何使用DecorView實現(xiàn)對話框功能,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

如果還不知道DecorView,那也沒有什么關(guān)系 ^_^

先來看看實現(xiàn)的效果

Android如何使用DecorView實現(xiàn)對話框功能

Android如何使用DecorView實現(xiàn)對話框功能

實現(xiàn)的大致思路

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

[下面大量 代碼 ]

第一個對話框的實現(xiàn)

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() {
      @Override
      public 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的時候?qū)⒉季痔砑拥紻ecorView上面去,dismiss的時候?qū)⒉季謴腄ecorView上面移除

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

public class TopTipDialog {
  private Activity activity;
  private View rootView;
  private boolean isShowing;
  private static final int VIEW_HEIGHT = 64;//px

  public 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() {
      @Override
      public void run() {
        TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,-VIEW_HEIGHT);
        translateAnimation1.setDuration(1500);
        translateAnimation1.setFillAfter(true);
        rootView.startAnimation(translateAnimation1);
      }
    },3000);

  }
}

看完上述內(nèi)容,是不是對Android如何使用DecorView實現(xiàn)對話框功能有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI