1. 自定义dialog实现
/**
* 显示支付窗口
*/
private void showDialog() {
dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
//填充对话框的布局
inflate = LayoutInflater.from(this).inflate(R.layout.item_dialog_pay, null);
// 初始化控件
TextView close = (TextView) inflate.findViewById(R.id.tv_supportpay_close);
LinearLayout weixin = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_weixin);
LinearLayout zhifubao = (LinearLayout) inflate.findViewById(R.id.ll_supportpay_zhifubao);
close.setOnClickListener(this);
weixin.setOnClickListener(this);
zhifubao.setOnClickListener(this);
// 将布局设置给Dialog
dialog.setContentView(inflate);
// 获取当前Activity所在的窗体
Window dialogWindow = dialog.getWindow();
//最重要的一句话,一定要加上!要不然怎么设置都不行
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
// 设置Dialog从窗体底部弹出
dialogWindow.setGravity(Gravity.BOTTOM);
// 获得窗体的属性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
Display d = dialogWindow.getWindowManager().getDefaultDisplay();
//获取屏幕宽
lp.width = (int) (d.getWidth());
//宽度按屏幕大小的百分比设置,这里我设置的是全屏显示
lp.gravity = Gravity.BOTTOM;
if (lp.gravity == Gravity.BOTTOM)
lp.y = 20;
//如果是底部显示,则距离底部的距离是0
dialogWindow.setAttributes(lp);
dialog.show();//显示对话框
}
2. 第三方控件实现bottomsheet
GitHub地址:https://github.com/Flipboard/bottomsheet
使用方法
- [ 1 ] 还是先依赖
repositories {
jcenter()
}
dependencies {
compile 'com.flipboard:bottomsheet-core:1.5.3'
compile 'com.flipboard:bottomsheet-commons:1.5.3' // optional
}
- [ 2 ] 在xml文件中使用
<?xml version="1.0" encoding="utf-8"?>
<com.flipboard.bottomsheet.BottomSheetLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/bottom_sheet_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:onClick="show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="三" />
</LinearLayout>
</com.flipboard.bottomsheet.BottomSheetLayout>
布局弹出内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF000"
android:orientation="vertical"
tools:context="com.itheima.app_bottom.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="条目一" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="条目二" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="条目三" />
</LinearLayout>
- [ 3 ] 编写显示与隐藏逻辑
public void show(View view) {
//判断弹出内容是否可见
if (bottomSheetLayout.isSheetShowing()) {
//可见则隐藏
bottomSheetLayout.dismissSheet();
} else {
if (popView == null) {
popView = LayoutInflater.from(this).inflate(R.layout.pop_sheet, bottomSheetLayout, false);
}
//不可见则显示 ,对popView进行是否为空判断可以减少view的创建
bottomSheetLayout.showWithSheetView(popView);
}
}