android基类Dialog

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.BaseAdapter;

import com.xh.boke.R;

/**
 * 自定义基类Dialog
 * Created by Administrator on 2015/10/21 0021.
 */
public class BaseDialog extends Dialog {

    protected Context mContext;
    protected BaseAdapter mAdapter;
    protected View convertView;
    protected int resId;    //view
    protected double percent; //屏幕宽度百分比

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    public BaseDialog(Context context, int resId,double percent) {
        super(context, R.style.common_custom_dialog);
        this.mContext=context;
        this.resId =resId;
        this.percent = percent;
    }

    public BaseDialog(Context context, int resId,BaseAdapter adapter,double percent) {
        super(context, R.style.common_custom_dialog);
        this.mContext=context;
        this.mAdapter = adapter;
        this.resId =resId;
        this.percent = percent;
    }

    protected void init() {
        //设置布局
        convertView = LayoutInflater.from(mContext).inflate(resId, null);
        this.setContentView(convertView);

        // 获取屏幕的宽高
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (dm.widthPixels * percent); // 宽度设置为屏幕的百分比
        this.getWindow().setAttributes(p);
    }
}

styles.xml

<resources>

    <!-- dialog默认样式 -->
    <style name="common_custom_dialog" parent="android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.6</item>
    </style>

</resources>

 

posted @ 2015-10-21 16:04  请输入...昵称  阅读(390)  评论(0编辑  收藏  举报