您的位置:首页 > 移动开发 > Android开发

Android 硬菜之圆角Dialog显示自定义布局(无棱角)

2016-09-11 22:04 477 查看
这几天很纳闷项目中要使用弹窗,但是 之前这个用过,但是没有做详细的笔记, 时间一久就忘了, 今天把这个问题有解决了. 开始使用的是alertdialog来显示自定义布局弹窗,但是有个问题, 就是当我给弹窗的布局设置shape圆角的时候整个弹窗会有自带的4个圆角,很不好, 货不多说,撸代码!


1.设置dialog的style

<style name="MyDialog" parent="@android:style/Theme.Dialog">

<!-- 边框 -->
<item name="android:windowFrame">@null</item>
<!-- 是否浮现在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 无标题 -->
<item name="android:windowNoTitle">true</item>
<!-- 背景透明 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 模糊 -->
<item name="android:windowContentOverlay">@null</item>
</style>


2.弹窗中要显示的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/top" />

<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/down" />

</LinearLayout>


3.activity中的代码

private void dialog() {
Dialog dialog = new Dialog(this,R.style.MyDialog);
View view = getLayoutInflater().inflate(R.layout.dialog_layout, null);
dialog.setContentView(view);
//设置弹窗宽高        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
//也可以这写,获取屏幕宽度减去100,相当于弹窗左右两边间距各是50,利用适配,写死不好
//dialog.getWindow().setLayout(UiUtils.getScreenHeight(mActivity).get(0)-100,WindowMan//ager.LayoutParams.WRAP_CONTENT);
dialog.show();

}


效果图:



注意: 设置宽高这里一定要看好了, 不然弹窗不能完全显示出来;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 布局 dialog