您好,登錄后才能下訂單哦!
這篇文章主要講解了Android如何使用DecorView實現對話框功能,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
如果還不知道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); } }
看完上述內容,是不是對Android如何使用DecorView實現對話框功能有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。