底部菜单实现(Dialog方案)
2024-09-01 17:12:15
项目中经常会要实现在屏幕底部弹出一个窗口,比如一个分享窗口:
下面详解实现步骤:
1.定义布局
<?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:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical"> <com.piston.usedcar.widget.KiloPiker
android:id="@+id/kp_car_grad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"/> <TextView
android:id="@+id/tv_sel_car_grad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/theme_gray_bg"
android:gravity="center"
android:padding="@dimen/common_padding_less"
android:text="确定"
android:textColor="@color/theme_gray_text"
android:textSize="@dimen/text_small"/>
</LinearLayout>
2.定义从底部弹出的动画
a.弹出动画dialog_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="100%p">
</translate>
b.退出动画dialog_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:toYDelta="100%p">
</translate>
3.在styles.xml定义动画样式
<style name="MyDialogAnimationStyle" parent="android:Animation">
<item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="@android:windowExitAnimation">@anim/dialog_exit</item>
</style>
4.防止dialog有边距,以便dialog填充整个屏幕宽度,定义如下样式:
<style name="common_dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
5.代码实现:
View view = LayoutInflater.from(AppContext.getContext()).inflate(R.layout.dialog_bottom_car_grad, null);
final Dialog dialog = new Dialog(this, R.style.common_dialog);
dialog.setContentView(view);
dialog.getWindow().setWindowAnimations(R.style.MyDialogAnimationStyle);
dialog.show(); final KiloPiker kpCarGrad = (KiloPiker) view.findViewById(R.id.kp_car_grad);
TextView mViewPengyou = (TextView) view.findViewById(R.id.tv_sel_car_grad); kpCarGrad.setNumberPickerDividerColor(kpCarGrad, R.color.background_split); kpCarGrad.setDisplayedValues(CAR_GRAD_ARRAY);
kpCarGrad.setMaxValue(CAR_GRAD_ARRAY.length - 1);
kpCarGrad.setMinValue(0); kpCarGrad.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
carGradSelPos = newVal;
}
}); mViewPengyou.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (carGradSelPos == 0) {
tvSynthCarGrad.setText("优秀车况");
} else if (carGradSelPos == 1) {
tvSynthCarGrad.setText("良好车况");
}
dialog.dismiss();
}
}); // 设置相关位置,一定要在 show()之后
Window window = dialog.getWindow();
window.getDecorView().setPadding(0, 0, 0, 0);
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
最新文章
- MySQL的if,case语句使用总结
- Nodejs简单介绍以及在windows环境下安装与配置流程
- centos安装CODEBLOCKS
- nova分析(3)—— nova-api
- db2数据库创建一张表,并为该表加上主键递增的性能和中间表的创建的sql语句
- CSS3实现半像素边框
- FreeMarker-Built-ins for numbers
- shell脚本应用(3)--语法结构
- 常用iptables规则整理
- lab3
- 【solr基础教程之二】索引
- JS离开页面 弹窗
- hdu 5437Alisha’s Party(优先队列)
- php设计模式--面向对象编程规范PSR
- 学习Python3基础知识过程中总结
- Python dict(或对象)与json之间的互相转化
- File类_常见的方法(获取,创建与删除,判断,重命名)
- 自学Linux Shell16.1-函数概念
- 永久修改VS include目录
- Redis 基础命令