android dialog 不全屏,Android关于自定义AlertDialog,设置不能充满全屏的解决方法

释放双眼,带上耳机,听听看~!

1.直接在Mainactivity中的oncreate中使用以下方法:发现dialog可以充满整个屏幕

onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

AlertDialog dialog=new AlertDialog.Builder(MainActivity.this).create();

dialog.show();//show要放在setContentView之前,否则会报错

Window window = dialog.getWindow();

// *** 主要就是在这里实现这种效果的.

// 设置窗口的内容页面,dialog_sex.xml文件中定义view内容

window.setContentView(R.layout.dialog_sex);

}

2在其他activity中使用1所述的方法,发现dialog显示不了全屏,会只充满1/5左右的宽度,高可以匹配到,宽度无法适应,采用windowmanager来获取当前activity的宽度,从而解决不能充满全屏的方法。dialog距离左右边距的问题可以在xml文件中进行设置,代码如下:

showSexDialog() {

AlertDialog dialog=new AlertDialog.Builder(ChangeUserInFoAty.this).create();

dialog.show();//show方法放在此处,如果先SetContentView之后在show会报错

WindowManager windowManager = getWindowManager();

Display display = windowManager.getDefaultDisplay();

WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

lp.width = (int)(display.getWidth()); //设置宽度

dialog.getWindow().setAttributes(lp);

dialog.getWindow().setContentView(R.layout.dialog_sex);

}

<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_marginLeft="10dip"

android:layout_marginRight="10dip"

android:background="@drawable/corners_bg"

android:orientation="vertical" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="20dip"

android:layout_marginTop="25dip"

android:text="选择性别"

android:textSize="25sp" />

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值