项目中经常会要实现在屏幕底部弹出一个窗口,比如一个分享窗口:

下面详解实现步骤:

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);

  

最新文章

  1. MySQL的if,case语句使用总结
  2. Nodejs简单介绍以及在windows环境下安装与配置流程
  3. centos安装CODEBLOCKS
  4. nova分析(3)—— nova-api
  5. db2数据库创建一张表,并为该表加上主键递增的性能和中间表的创建的sql语句
  6. CSS3实现半像素边框
  7. FreeMarker-Built-ins for numbers
  8. shell脚本应用(3)--语法结构
  9. 常用iptables规则整理
  10. lab3
  11. 【solr基础教程之二】索引
  12. JS离开页面 弹窗
  13. hdu 5437Alisha’s Party(优先队列)
  14. php设计模式--面向对象编程规范PSR
  15. 学习Python3基础知识过程中总结
  16. Python dict(或对象)与json之间的互相转化
  17. File类_常见的方法(获取,创建与删除,判断,重命名)
  18. 自学Linux Shell16.1-函数概念
  19. 永久修改VS include目录
  20. Redis 基础命令

热门文章

  1. linux fg bg ctrl + z jobs &amp; 等命令
  2. 原生js获取屏幕的宽高
  3. webservice跨域文件,好多年前的东西,远程调用,js服务器端使用,可以远程调用
  4. glGenTextures
  5. UVA 10803 Thunder Mountain
  6. Android (Notification)消息推送机制
  7. JAVA反映练手
  8. 用了GradientDrawable后,当点击控件时,控件大小发生变化
  9. 洛谷 P1048 采药【裸01背包】
  10. King of Karaoke