Android设置Dialog的屏幕位置、背景、大小

1. 设置Dialog在窗口底部,且宽度最大。
可以使用 DialogFragment实现底部弹窗布局( http://blog.csdn.net/caroline_wendy/article/details/51792532  )。
Window window = getDialog().getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.gravity = Gravity.BOTTOM;
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
 或 使用Dialog可类似处理,进行屏幕位置的设置。
Window window = getDialog().getWindow();
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.gravity = Gravity.CENTER; //居中显示
window.setAttributes(layoutParams);

设置对话框外部背景
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //去掉背景色
设置主题
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

实例:
public class WifiListDialog extends DialogFragment {
    private Context mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wifi_dialog, null);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //去掉背景色(一些设备上由于系统主题原因会有背景边框)

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.BOTTOM;
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);

        initView(view);
        return view;
    }
   ...
}



2. Activity也可作为Dialog使用,使用Activity作为对话框使用时,若希望宽度占满屏幕,则需要进行如下设置:
设置Activity主题(以Dialog风格显示):
android:name=".module.homepage.ActivitySelectDevice"
    android:theme="@style/ActivityDialog"
    android:launchMode="singleTop"/>
在Activity的oncreate()方法中设置宽度占满屏(同理,可设置宽度具体大小)
public class ActivitySelectDevice extends Activity {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xxx);

        Display display = getWindowManager().getDefaultDisplay();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = display.getWidth();
        getWindow().setAttributes(layoutParams);

    }
}
   或 (这两种方式实际是一样的)
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
getWindow().setLayout(dm.widthPixels, getWindow().getAttributes().height);

注意:如果只是设置了布局宽度,而没有将 android:windowBackground 属性置为 null,那么对话框宽度不能占满屏,会有边界区域。


3. 设置Dialog的大小(宽高)
public class AppDialog extends Dialog {

    public AppDialog(Context context) {
        super(context, R.style.dialogTheme);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_xxx, null);

        setContentView(view);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay(); //获取屏幕宽高
        Point point = new Point();
        display.getSize(point);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes(); //获取当前对话框的参数值
        layoutParams.width = (int) (point.x * 0.5); //宽度设置为屏幕宽度的0.5
        layoutParams.height = (int) (point.y * 0.5); //高度设置为屏幕高度的0.5
//        layoutParams.width = (int) (display.getWidth() * 0.5);
//        layoutParams.height = (int) (display.getHeight() * 0.5);
        window.setAttributes(layoutParams);
    }
}



你可能感兴趣的:(Android开发)