1. 自定義dialog實(shí)現(xiàn)
/**
* 顯示支付窗口
*/
private void showDialog() {
dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
//填充對(duì)話框的布局
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);
// 將布局設(shè)置給Dialog
dialog.setContentView(inflate);
// 獲取當(dāng)前Activity所在的窗體
Window dialogWindow = dialog.getWindow();
//最重要的一句話,一定要加上!要不然怎么設(shè)置都不行
dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
// 設(shè)置Dialog從窗體底部彈出
dialogWindow.setGravity(Gravity.BOTTOM);
// 獲得窗體的屬性
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
Display d = dialogWindow.getWindowManager().getDefaultDisplay();
//獲取屏幕寬
lp.width = (int) (d.getWidth());
//寬度按屏幕大小的百分比設(shè)置,這里我設(shè)置的是全屏顯示
lp.gravity = Gravity.BOTTOM;
if (lp.gravity == Gravity.BOTTOM)
lp.y = 20;
//如果是底部顯示,則距離底部的距離是0
dialogWindow.setAttributes(lp);
dialog.show();//顯示對(duì)話框
}
2. 第三方控件實(shí)現(xiàn)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>
布局彈出內(nèi)容
<?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) {
//判斷彈出內(nèi)容是否可見
if (bottomSheetLayout.isSheetShowing()) {
//可見則隱藏
bottomSheetLayout.dismissSheet();
} else {
if (popView == null) {
popView = LayoutInflater.from(this).inflate(R.layout.pop_sheet, bottomSheetLayout, false);
}
//不可見則顯示 ,對(duì)popView進(jìn)行是否為空判斷可以減少view的創(chuàng)建
bottomSheetLayout.showWithSheetView(popView);
}
}