我們使用Dialog來實現(xiàn)中間彈框和底部彈框兩種效果
第一種:底部彈框
效果如下圖:
1蝙搔、定義DemoDialog,繼承Dialog
public class DemoDialog extends Dialog {
DemoDialogClickCallback demoDialogClickCallback;
Context context;
String title;
String content;
public DemoDialog(@NonNull Context context, String title, String content,DemoDialogClickCallback demoDialogClickCallback) {
super(context);
this.context = context;
this.content = content;
this.title = title;
this.demoDialogClickCallback=demoDialogClickCallback;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_demo);
Window window = getWindow();
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//底部彈框效果
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.BOTTOM);
window.setWindowAnimations(R.style.animate_dialog);
//中間彈框效果
// window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// window.setGravity(Gravity.CENTER);
TextView titleView=findViewById(R.id.title);
TextView contentView=findViewById(R.id.content);
Button buttonView=findViewById(R.id.i_know);
buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
demoDialogClickCallback.iKonw();
}
});
titleView.setText(this.title);
contentView.setText(this.content);
setCanceledOnTouchOutside(false);
}
public interface DemoDialogClickCallback {
public void iKonw();
}
}
2、設置彈框的入場出場動畫 animate_dialog
<style name="animate_dialog">
<item name="android:windowEnterAnimation">@anim/anim_bottom_in</item>
<item name="android:windowExitAnimation">@anim/anim_bottom_out</item>
</style>
3、調(diào)用方法
demoDialog = new DemoDialog(this, "小區(qū)公告", "今天晚上在惠民廣場唬滑,紅星樂隊驚艷演出澈圈,同志們都來參見!巴帮!", new DemoDialog.DemoDialogClickCallback() {
@Override
public void iKonw() {
demoDialog.dismiss();
}
});
demoDialog.show();
第二種:中間彈框
效果如下:
彈框位置控制的關(guān)鍵屬性
把
window.setGravity(Gravity.BOTTOM);
改成
window.setGravity(Gravity.CENTER);
即可實現(xiàn)