自定義PopupWindow和AlertDialog

PopupWindow是一個常用的一個組件,和popwindow相似的組件,還有一個叫AlertDialog

PopupWindow的自定義相信大家一定很熟悉,這裏我展示一下我的相關代碼了,如有不足請指教哦

PopupWindow pop=new PopupWindow(context);
        View contentView = LayoutInflater.from(context).inflate(R.layout.navigationpopup, null);  //獲取popupwindow樣式
        pop.setContentView(contentView);
        pop.setWidth(LayoutParams.MATCH_PARENT);// 設置寬度
        pop.setHeight(LayoutParams.WRAP_CONTENT);//設置高度
        //初始化layout 上面的view
        Button btn=(Button)contentView.findViewById(R.id.btn);
        //添加點擊事件--省略
pop.setAnimationStyle(R.style.AnimationFade_bottom);//設置動畫
        pop.setOutsideTouchable(true);//設置outside touch
        pop.getBackground().setAlpha(0);//設置背景爲透明
        pop.showAtLocation(context.findViewById(R.id.ll_web), Gravity.BOTTOM, 0, 0);//設置顯示

彈出動畫展示給大家:

<style name="AnimationFade_bottom">

        <!-- PopupWindow_bottom上下彈出的效果 -->
        <item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>
        <item name="android:windowExitAnimation">@anim/out_toptobottom</item>
    </style>

@anim/in_bottomtotop

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定義從上向下進入的動畫 -->
    <translate
        android:duration="200"
        android:fromYDelta="100%"
        android:toYDelta="0" />

</set>

@anim/out_toptobottom

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- 定義從下向上進入的動畫 -->
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="100%" />

</set>

自定義的樣式主要看你的layout怎麼寫了

這裏再寫一個自定義的AlertDialog,

這裏我們注意到Layout的作用,當我們自定義Alert 的時候就能夠對於Alert的樣式進行修改,從而達到自定義的目的。

// 取得自定義View    
        LayoutInflater layoutInflater = LayoutInflater.from(this);   
        View myLoginView = layoutInflater.inflate(R.layout.login, null);   

        Dialog alertDialog = new AlertDialog.Builder(this).   
                setTitle("用戶登錄").   
                setIcon(R.drawable.ic_launcher).   
                setView(myLoginView).   
                setPositiveButton("登錄", new DialogInterface.OnClickListener() {   

                    @Override   
                    public void onClick(DialogInterface dialog, int which) {   
                        // TODO Auto-generated method stub    
                    }   
                }).   
                setNegativeButton("取消", new DialogInterface.OnClickListener() {   

                    @Override   
                    public void onClick(DialogInterface dialog, int which) {   
                        // TODO Auto-generated method stub    
                    }   
                }).   
                create();   
        alertDialog.show();  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章