Android 彈出式佈局之Dialog的使用
阿新 • • 發佈:2018-12-20
文章目錄
Dialog的基本使用
官方不建議直接使用Dialog建立對話方塊,一般情況下使用子類AlertDialog代替使用,在專案實際開發中的話,建議使用FragmentDialog,方便生命週期的管理和複用。
1-1、dialog基本使用
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this) .setTitle("這是標題") .setMessage("這是具體的訊息內容") .setPositiveButton("確認", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //確認按鈕 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //訊息按鈕 } }) .create(); dialog.show();
1-2、自定義dialog
public class CustomDialog extends Dialog { public CustomDialog(@NonNull Context context) { super(context); View layout = LayoutInflater.from(context) .inflate(R.layout.dialog_layout, null); setContentView(layout); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.width = WindowManager.LayoutParams.MATCH_PARENT; params.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, getContext().getResources().getDisplayMetrics()); window.setAttributes(params); } }
1-3、dialog寬度完全填充螢幕
Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_layout);
dialog.show();
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);
//設定了填充父控制元件,但是沒有貼邊的原因就是存在decorView的padding
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams layoutParams = window.getAttributes();
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(layoutParams);
//必須設定,否則沒有效果
window.getDecorView().setBackgroundColor(Color.parseColor("#FFFFFF"));
1-4、給dialog新增進入移除動畫效果
設定動畫檔案
# 進入動畫效果 window_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="800"
android:fromXDelta="0"
android:fromYDelta="100%"
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="800"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="100%" />
</set>
設定動畫屬性
# 在value/style.xml 下定義 dialogWindowAnimator
<style name="dialogWindowAnimator" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/window_enter</item>
<item name="android:windowExitAnimation">@anim/window_out</item>
</style>
將設定的動畫屬性加入到程式碼中進行設定
window.setWindowAnimations(R.style.dialogWindowAnimator);
1-5、官方推薦DialogFragment使用
傳統的new AlertDialog在螢幕旋轉時,第一不會儲存使用者輸入的值,第二還會報異常,因為Activity銷燬前不允許對話方塊未關閉。而通過DialogFragment實現的對話方塊則可以完全不必考慮旋轉的問題。
使用DialogFragment建立dialog有兩種,一種是使用onCreateView
按照建立佈局的形式建立,另一種是使用onCreateDialog
使用AlertDialog或者Dialog建立。
使用onCreateView
public class CustomDialogFragment extends DialogFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
Window window = getDialog().getWindow();
WindowManager.LayoutParams params = window.getAttributes();
window.getDecorView().setPadding(0, 0, 0, 0);
params.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(params);
window.getDecorView().setBackgroundColor(Color.WHITE);
//隱藏狀態列
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
return inflater.inflate(R.layout.dialog_layout, container);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//do somethings
}
}
使用onCreateDialog
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog dialog = new AlertDialog.Builder(getContext())
//~~~
.create();
return dialog;
}
1-6、其它使用
- 設定三個按鈕,使用
setNeutralButton
- 填充一個列表,使用
setItems
傳入一個字串集合 - 設定選單列表,使用
setSingleChoiceItems
- 設定多選列表,使用
setMultiChoiceItems
參考
1、https://blog.csdn.net/lmj623565791/article/details/37815413