最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼

 更新時(shí)間:2017年03月20日 11:00:56   作者:xiaoyanger  
本篇文章主要介紹了Android實(shí)現(xiàn)底部對(duì)話框BottomDialog代碼。這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。

最近項(xiàng)目上需要實(shí)現(xiàn)一個(gè)底部對(duì)話框,要實(shí)現(xiàn)這樣的功能其實(shí)很簡(jiǎn)單,先看代碼:

private void show1() {
 Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
 View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null);
 bottomDialog.setContentView(contentView);
 ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
 layoutParams.width = getResources().getDisplayMetrics().widthPixels;
 contentView.setLayoutParams(layoutParams);
 bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
 bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
 bottomDialog.show();
}

對(duì)話框的樣式style:

<style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog">
 <item name="android:windowNoTitle">true</item>
 <item name="android:windowBackground">@android:color/transparent</item>
</style>

在對(duì)話框中的按鈕需要MD風(fēng)格的波紋效果的話,對(duì)話框的style的parent需要設(shè)定parent="@style/Base.V7.Theme.AppCompat.Light.Dialog",否則沒(méi)有效果。同時(shí)將對(duì)話框所在window的標(biāo)題去掉。android:windowBackground屬性一定要設(shè)置成透明,否則自定義形狀的對(duì)話框背景就是默認(rèn)的白色了。如果不設(shè)置為透明,比如我們通常要設(shè)置的圓角對(duì)話框就沒(méi)有效果。

對(duì)話框顯示時(shí)從底部進(jìn)入,關(guān)閉時(shí)從底部滑出。動(dòng)畫(huà)樣式:

<style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog">
 <item name="android:windowEnterAnimation">@anim/translate_dialog_in</item>
 <item name="android:windowExitAnimation">@anim/translate_dialog_out</item>
</style>

tranlate_dialog_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="300"
   android:fromXDelta="0"
   android:fromYDelta="100%"
   android:toXDelta="0"
   android:toYDelta="0">
</translate>

tranlate_dialog_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="300"
   android:fromXDelta="0"
   android:fromYDelta="0"
   android:toXDelta="0"
   android:toYDelta="100%">
</translate>

實(shí)現(xiàn)底部對(duì)話框的原理就是修改對(duì)話框的內(nèi)容布局contentView的參數(shù),使它的寬度剛好等于屏幕的寬度,并且設(shè)置對(duì)話框所在Window的gravity屬性為bottom。

需要注意的是,上面代碼中需要在調(diào)用contentView.getLayoutParams()需要在setContentView方法后,否則獲取到的LayoutParams為null,當(dāng)然也可以自己new一個(gè)LayoutParams設(shè)置給contentView。


如果是要實(shí)現(xiàn)底部圓角對(duì)話框,原理也相似,只需要給contentView添加一個(gè)圓角的背景shape,并減小contentView的寬度給左右兩邊留一定的距離,同時(shí)給底部設(shè)置邊距。

private void show2() {
 Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
 View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_circle, null);
 bottomDialog.setContentView(contentView);
 ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
 params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(this, 16f);
 params.bottomMargin = DensityUtil.dp2px(this, 8f);
 contentView.setLayoutParams(params);
 bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
 bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
 bottomDialog.show();
}

 源碼:BottomDialog_jb51.rar

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

龙游县| 河南省| 保亭| 岗巴县| 灵石县| 阳原县| 新巴尔虎右旗| 舞钢市| 汝城县| 肥乡县| 潼关县| 武山县| 聂荣县| 鹤壁市| 吐鲁番市| 彝良县| 长春市| 开江县| 集贤县| 囊谦县| 永安市| 汉阴县| 齐齐哈尔市| 丰镇市| 合肥市| 福清市| 长沙县| 临朐县| 莱州市| 石泉县| 水富县| 电白县| 广宁县| 彭州市| 金堂县| 信阳市| 贺州市| 南通市| 阿城市| 宁安市| 横山县|