国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android巧用DecorView實現對話框功能

2019-10-23 18:32:07
字體:
來源:轉載
供稿:網友

如果還不知道DecorView,那也沒有什么關系 ^_^

先來看看實現的效果

android,decorview,實現對話框,對話框

android,decorview,實現對話框,對話框

實現的大致思路

  1. 首先需要明白什么是DecorView,他是android中界面的根布局。其實android的activity界面整個就是一個控件樹,DecorView是根節點,DecorView的孩子節點就是一個LinearLayout,這個LinearLayout的孩子系節點就包括狀態欄 + 和我們自己寫的布局
  2. DecorView是FramLayout的子類(可以從源碼中看到)
  3. 既然DecorView是根節點,而且還是FrameLayout,所以我們可以把我們自己的布局 添加到DecorView 或者 從DecorView移除,這樣就模擬出了一個Dialog的效果~~ ,當然這個Dialog的樣式,動畫就可以自己想怎么寫就怎么寫了撒
  4. 通過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() {      @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的時候將布局添加到DecorView上面去,dismiss的時候將布局從DecorView上面移除

提示的實現(沒有處理完善~~ 僅僅就是說明哈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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吴堡县| 噶尔县| 台南县| 布尔津县| 石首市| 丹凤县| 贡山| 河东区| 青神县| 宜兰市| 周至县| 麻江县| 崇信县| 竹山县| 赤峰市| 筠连县| 科尔| 赤峰市| 临西县| 灵宝市| 江都市| 关岭| 鄂托克旗| 江西省| 福鼎市| 常山县| 绥滨县| 哈尔滨市| 晋江市| 儋州市| 永川市| 虞城县| 合肥市| 贵德县| 渑池县| 鱼台县| 阿荣旗| 酉阳| 宾川县| 冕宁县| 辰溪县|