Android的Dialog和PopupWindow的使用

1、Dialog及设置Dialog的动画

设置Dialog的位置和大小与加载的布局文件无关。需自己设置dialog参数。
1)设置Dialog位置:

      设置位置时必须先指定Dialog的gravity属性,否则指定大小无用。

/*
    * lp.x与lp.y表示相对于原始位置的偏移.
    * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
    * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
    * 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
    * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
    * 当参数值包含Gravity.CENTER_HORIZONTAL时
    * ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
    * 当参数值包含Gravity.CENTER_VERTICAL时
    * ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
    * gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
    * Gravity.CENTER_VERTICAL.
    * 
    * 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
    * 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,
    * Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
*/


2)去标题:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);<span style="color:#cc0000;">//要在创建完dialog后就调用,否则报错

3)设置Dialog的宽和高

WindowManager wm = getWindowManager();  
Display display = wm.getDefaultDisplay();  
android.view.WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();     
lp.width = display.getWidth();  
lp.height =LayoutParams.WRAP_CONTENT;  
dialog.getWindow().setAttributes(lp); 

4)设置动画

设置Dialog的动画只能通过设置xml的形式,然后设置在style中,最后在代码中设置。

2)贴代码

第一步:写动画xml

window_in.xml:

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android" >    
<translate    
        android:duration="500"    
        android:fromXDelta="0"    
        android:fromYDelta="1000"    
        android:toXDelta="0"    
        android:toYDelta="0" />    
</set> 

window_out.xml:

<?xml version="1.0" encoding="utf-8"?>    
<set xmlns:android="http://schemas.android.com/apk/res/android" >    
<translate    
        android:duration="500"    
        android:fromXDelta="0"    
        android:fromYDelta="0"    
        android:toXDelta="0"    
        android:toYDelta="1000" />    
</set>    

第二步:动画配置到style中

<style name="main_menu_animstyle">    
       <item name="android:windowEnterAnimation">@anim/settingswindow_in_anim</item>    
       <item name="android:windowExitAnimation">@anim/settingswindow_out_anim</item>    
</style> 


第三步:将动画用于dialog中

Window window = dialog.getWindow();    
//设置显示动画    
window.setWindowAnimations(R.style.main_menu_animstyle);    
WindowManager.LayoutParams wl = window.getAttributes();    
wl.x = 0;    
wl.y = getWindowManager().getDefaultDisplay().getHeight();    
//设置显示位置    
dialog.onWindowAttributesChanged(wl);//设置点击外围解散    
dialog.setCanceledOnTouchOutside(true);    
dialog.show();

2、Popupwindow

1)设置显示位置特别方便:

showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移。
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移。
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移。

2)点击Popupwindow以外区域自动消失

注意一定要设置backgroundDrawable

//参数也可以是下面这俩值  
//1、getResources().getDrawable(R.drawable.abc)  
//2、getWallpaper()  
//当你发现有背景色时,需给布局文件设置背景色,这样即可覆盖系统自带的背景色。  
pw.setBackgroundDrawable(new BitmapDrawable());  
pw.setOutsideTouchable(true);  

3)完整用法代码:

用原生的popupwindow

View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.popup, null);  
PopupWindow pw=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,300);  
//  pw.setFocusable(true);------完全没用啊  
//参数也可以是下面这俩值  
//1、getResources().getDrawable(R.drawable.abc)  
//2、getWallpaper()  
//当你发现有背景色时,需给布局文件设置背景色,这样即可覆盖系统自带的背景色。  
pw.setBackgroundDrawable(new BitmapDrawable());  
pw.setOutsideTouchable(true);  
pw.showAsDropDown(findViewById(R.id.btn), 0,50);  
//pw.showAtLocation(findViewById(R.id.btn), Gravity.BOTTOM, 0, 0); 

    写一个类去继承popupwindow

public class BasePopupWindow extends PopupWindow {
    protected int SCREEN_WIDTH;
    protected int SCREEN_HEIGHT;

    public BasePopupWindow(Context context) {
        super(context);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        SCREEN_WIDTH = wm.getDefaultDisplay().getWidth();
        SCREEN_HEIGHT = wm.getDefaultDisplay().getHeight();
    }
}
public class CitySelectPopWindows extends BasePopupWindow {

    private TextView tx1;
    private View mMenuView;
    private LayoutInflater inflater;
    private List<Serializable> arrayList;
    private CityPicker cityPicker;
    RelativeLayout r1;
    private Context mContext;
    private Activity mActivity;
    private int p = 0;
    private String city;
    private String Area;
    InterfaceA interfaceA;

    public CitySelectPopWindows(Activity activity, Context context, int p) {
        super(context);
        inflater = LayoutInflater.from(context);
        mMenuView = inflater.inflate(R.layout.dialogview, null);
        cityPicker = (CityPicker) mMenuView.findViewById(R.id.citypicker);
        r1 = (RelativeLayout) mMenuView.findViewById(R.id.r1);
        mContext = context;
        mActivity = activity;
        this.p = p;
        this.arrayList = new ArrayList<Serializable>();
        prepareData();
        setContentView(mMenuView);
//        setWidth(SCREEN_WIDTH);
//        setHeight(DensityUtil.dip2px(context, 260));
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setAnimationStyle(R.style.bottomDialog);
//        ColorDrawable dw = new ColorDrawable(0000000000);
//        setBackgroundDrawable(dw);
    }

    private void prepareData() {
        Button choose_cancel2 = (Button) mMenuView.findViewById(R.id.cancel);
        Button choose_ok2 = (Button) mMenuView.findViewById(R.id.ok);
        choose_cancel2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        choose_ok2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                city_string = cityPicker.getCity_string();
//                province = cityPicker.getProvince_string();
//                city = cityPicker.getcity_string();
//                area = cityPicker.getArea_string();
//                setLocationMessage(city_string, province, city, area);

                Area = cityPicker.getCity_Area();
                city = cityPicker.getCity_city();
                dismiss();
//                mActivity.setCity(city);
            }
        });
    }
调用的时候:

     cs = new CitySelectPopWindows(this,getApplicationContext(),0);
        cs.settext(this);
        cs.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
        re1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
<span style="white-space:pre">		</span>//依赖哪个控件来显示,还有显示的位置
                cs.showAtLocation(lin, Gravity.BOTTOM, 0, 0);

            }
        });






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值