Android 从下往上弹出 DialogFragment 的关键代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//java代码:
public class CountrySelectDialog extends AppCompatDialogFragment{
 
  public static CountrySelectDialog newInstance() {
    Bundle args = new Bundle();
    CountrySelectDialog fragment = new CountrySelectDialog();
    fragment.setArguments(args);
    return fragment;
  }
 
  @Override
  public void onStart() {
    super.onStart();
    setStyle(AppCompatDialogFragment.STYLE_NORMAL, R.style.BottomDialog);//设置自定义style
 
    Window window = getDialog().getWindow();
    window.setBackgroundDrawable(null);//必须的
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.gravity = Gravity.BOTTOM;//在下边
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    window.setAttributes(lp);
 
    getDialog().setOnDismissListener(dialog -> {
        //todo,关闭dialog的回调监听
      }
    });
 
  }
 
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.xxx, container, false);
  }
 
  public void show(FragmentManager supportFragmentManager) {
    show(supportFragmentManager, getClass().getName());
    setStyle(AppCompatDialogFragment.STYLE_NORMAL, R.style.BottomDialog);//show的时候设置,不然第一次style无效
  }
 
}
 
 
//styles.xml代码:
<style name="BottomDialog" parent="Theme.AppCompat.Light.Dialog">
  <item name="android:windowAnimationStyle">@style/BottomDialogAnimation</item>
</style>
 
<style name="BottomDialogAnimation">
  <item name="android:windowEnterAnimation">@anim/bottom_dialog_in</item>
  <item name="android:windowExitAnimation">@anim/bottom_dialog_out</item>
</style>
 
 
// res/anim/bottom_dialog_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/accelerate_interpolator"
  >
  <translate
    android:duration="300"
    android:fillAfter="true"
    android:fromXDelta="0"
    android:fromYDelta="100%"
    android:toXDelta="0"
    android:toYDelta="0">
 
  </translate>
  <alpha
    android:duration="300"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"/>
</set>
 
// res/anim/bottom_dialog_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/accelerate_interpolator">
  <translate
    android:duration="300"
    android:fillAfter="true"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="0"
    android:toYDelta="100%">
 
  </translate>
  <alpha
    android:duration="300"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"/>
</set>

  

posted @   yongfengnice  阅读(688)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示