1. 程式人生 > >Android設定Dialog的螢幕位置、背景、大小

Android設定Dialog的螢幕位置、背景、大小

1. 設定Dialog在視窗底部,且寬度最大。
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風格顯示):
<style name="ActivityDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@null</item> //必須設定該屬性為空,才能佔滿螢幕,否則會有邊界空白</style>
<activity 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);
}
}