Android 自定義dialog出現的位置


跟 https://www.cnblogs.com/guochangxin/p/11457537.html 配套

 

1、定義一個dialog的類

public class OtherDialog {
private View view;

public Dialog showOtherDialog(Context context) {
//1、使用Dialog、設置style
final Dialog dialog = new Dialog(context, R.style.DialogTheme);
//2、設置布局
view = View.inflate(context, R.layout.news_bottom_dialog, null);
dialog.setContentView(view);

Window window = dialog.getWindow();
//設置彈出位置
window.setGravity(Gravity.TOP);

int matchParent = ViewGroup.LayoutParams.MATCH_PARENT;//父布局的寬度

Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.TOP | Gravity.RIGHT);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = matchParent;
lp.height = matchParent;
lp.x = matchParent;
lp.y = 300; //設置出現的高度,距離頂部
window.setAttributes(lp);

//設置彈出動畫
// window.setWindowAnimations(R.style.main_menu_animStyle);
//設置對話框大小
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.show();

return dialog;


}
}

2、使用
private OtherDialog otherDialog=new OtherDialog();//實例化
otherDialog.showOtherDialog(getContext());



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM