当前位置: 移动技术网 > IT编程>移动开发>Android > Android 从底部弹出Dialog(横向满屏)的实例代码

Android 从底部弹出Dialog(横向满屏)的实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

今日白银价格,省钱通网络电话下载,大海まりん

项目中经常需要底部弹出框,这里我整理一下其中我用的比较顺手的一个方式(底部弹出一个横向满屏的dialog)。

效果图如下所示(只显示关键部分):

步骤如下所示:

1.定义一个dialog的布局(lay_share.xml)

<?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="match_parent"
android:background="@color/white"
android:orientation="vertical">
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingbottom="@dimen/padding_15"
android:paddingtop="@dimen/padding_15">
<view
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablepadding="@dimen/padding_5"
android:drawabletop="@mipmap/ic_weixin_share"
android:gravity="center"
android:text="微信"
android:textcolor="@color/color_999999"
android:textsize="@dimen/text_14" />
<view
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablepadding="@dimen/padding_5"
android:drawabletop="@mipmap/ic_circle_share"
android:gravity="center"
android:text="朋友圈"
android:textcolor="@color/color_999999"
android:textsize="@dimen/text_14" />
<view
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablepadding="@dimen/padding_5"
android:drawabletop="@mipmap/ic_weibo_share"
android:gravity="center"
android:text="微博"
android:textcolor="@color/color_999999"
android:textsize="@dimen/text_14" />
<view
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
</linearlayout>
<view
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginleft="@dimen/padding_10"
android:layout_marginright="@dimen/padding_10"
android:background="@color/color_c9c9c9" />
<textview
android:id="@+id/tv_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/padding_15"
android:text="取消"
android:textcolor="@color/color_666666"
android:textsize="@dimen/text_18" />
</linearlayout>

2.定义弹出框弹出动画(dialog_enter.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromydelta="100%p"
android:toydelta="0" />
</set>
dialog_enter.xml

3.定义弹出框隐藏动画(dialog_exit.xml)

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromydelta="0"
android:toydelta="100%p" />
</set>
dialog_exit.xml

4.定义动画style

<!--弹出框动画-->
<style name="share_animation" parent="android:animation">
<item name="android:windowenteranimation">@anim/dialog_enter</item>
<item name="android:windowexitanimation">@anim/dialog_exit</item>
</style>

5.定义对话框样式

<!-- 对话框样式 -->
<style name="dialog_bottom_full" parent="android:style/theme.dialog">
<item name="android:windowbackground">@android:color/transparent</item>
<item name="android:windownotitle">true</item>
<item name="android:windowisfloating">true</item>
<item name="android:windowcontentoverlay">@null</item>
<item name="android:scrollhorizontally">true</item>
</style>

6.最后,在需要从底部弹出dialog的地方,直接调用showdialog()方法

/**
* 显示分享弹出框
*/
private void showdialog() {
if (msharedialog == null) {
initsharedialog();
}
msharedialog.show();
}
/**
* 初始化分享弹出框
*/
private void initsharedialog() {
msharedialog = new dialog(this, r.style.dialog_bottom_full);
msharedialog.setcanceledontouchoutside(true);
msharedialog.setcancelable(true);
window window = msharedialog.getwindow();
window.setgravity(gravity.bottom);
window.setwindowanimations(r.style.share_animation);
view view = view.inflate(this, r.layout.lay_share, null);
view.findviewbyid(r.id.tv_cancel).setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
if (msharedialog != null && msharedialog.isshowing()) {
msharedialog.dismiss();
}
}
});
window.setcontentview(view);
window.setlayout(windowmanager.layoutparams.match_parent, windowmanager.layoutparams.wrap_content);//设置横向全屏
}

以上所述是小编给大家介绍的android 从底部弹出dialog(横向满屏)的实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网