当前位置:   article > 正文

Android编程:底部弹出的对话框_android studio 底部半屏对话框

android studio 底部半屏对话框

Android编程:底部弹出的对话框


本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


环境:

主机:WIN10

开发环境:Android Studio 2.2 Preview 3


说明:

两种方法实现底部弹出的对话框:

  • Dialog
  • DialogFragment
推荐用DialogFragment

效果图:



布局文件dialog_select_call.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <RelativeLayout
  6. android:id="@+id/layout_voice"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:background="@color/white">
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentStart="true"
  14. android:layout_centerInParent="true"
  15. android:textSize="16sp"
  16. android:textColor="@color/black"
  17. android:layout_marginLeft="16dp"
  18. android:layout_marginRight="16dp"
  19. android:layout_marginTop="16dp"
  20. android:layout_marginBottom="16dp"
  21. android:text="语音课堂"/>
  22. </RelativeLayout>
  23. <View
  24. android:layout_width="fill_parent"
  25. android:layout_height="0.1dp"
  26. android:background="#b4b4b4"
  27. android:layout_marginLeft="16dp"
  28. android:layout_marginRight="16dp"/>
  29. <RelativeLayout
  30. android:id="@+id/layout_video"
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:background="@color/white">
  34. <TextView
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_alignParentStart="true"
  38. android:layout_centerInParent="true"
  39. android:textSize="16sp"
  40. android:textColor="@color/black"
  41. android:layout_marginLeft="16dp"
  42. android:layout_marginRight="16dp"
  43. android:layout_marginTop="16dp"
  44. android:layout_marginBottom="16dp"
  45. android:text="视频课堂"/>
  46. </RelativeLayout>
  47. <View
  48. android:layout_width="fill_parent"
  49. android:layout_height="0.1dp"
  50. android:background="#b4b4b4"
  51. android:layout_marginLeft="16dp"
  52. android:layout_marginRight="16dp"/>
  53. <RelativeLayout
  54. android:layout_width="match_parent"
  55. android:layout_height="wrap_content"
  56. android:background="@color/white">
  57. <Button
  58. android:id="@+id/cancel"
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:text="取消"
  62. android:layout_marginLeft="16dp"
  63. android:layout_marginRight="16dp"
  64. android:layout_marginTop="16dp"
  65. android:layout_marginBottom="16dp"/>
  66. </RelativeLayout>
  67. </LinearLayout>


Dialog实现源码:

初始化:

  1. private void dialogSelectCallInit() {
  2. dialogSelectCall = new Dialog(this, R.style.DialogPopBottom);
  3. View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_select_call, null);
  4. dialogSelectCall.setContentView(inflate);
  5. Window dialogWindow = dialogSelectCall.getWindow();
  6. dialogWindow.setGravity(Gravity.BOTTOM);
  7. WindowManager.LayoutParams lp = dialogWindow.getAttributes();
  8. lp.x = 0;
  9. lp.y = 0;
  10. lp.width = getResources().getDisplayMetrics().widthPixels;
  11. dialogWindow.setAttributes(lp);
  12. RelativeLayout layoutVoice = (RelativeLayout) inflate.findViewById(R.id.layout_voice);
  13. RelativeLayout layoutVideo = (RelativeLayout) inflate.findViewById(R.id.layout_video);
  14. Button buttonCancel = (Button) inflate.findViewById(R.id.cancel);
  15. RxView.clicks(layoutVoice)
  16. .throttleFirst(2, TimeUnit.SECONDS)
  17. .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
  18. .subscribe(v -> {
  19. // dialogSelectCall.cancel();
  20. VoiceSessionActivity.startActivityCallOut(this, userId);
  21. });
  22. RxView.clicks(layoutVideo)
  23. .throttleFirst(2, TimeUnit.SECONDS)
  24. .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
  25. .subscribe(v -> {
  26. // dialogSelectCall.cancel();
  27. // VideoSessionActivity.startActivityCallOut(this, userId);
  28. });
  29. RxView.clicks(buttonCancel)
  30. .throttleFirst(2, TimeUnit.SECONDS)
  31. .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
  32. .subscribe(v -> dialogSelectCall.cancel());
  33. RxView.touches(layoutVoice, motionEvent -> {
  34. dealLayoutTouch(layoutVoice, motionEvent);
  35. return false;
  36. }).compose(this.bindUntilEvent(ActivityEvent.DESTROY)).subscribe(motionEvent -> {});
  37. RxView.touches(layoutVideo, motionEvent -> {
  38. dealLayoutTouch(layoutVideo, motionEvent);
  39. return false;
  40. }).compose(this.bindUntilEvent(ActivityEvent.DESTROY)).subscribe(motionEvent -> {});
  41. }
  42. private void dealLayoutTouch(View v, MotionEvent event) {
  43. switch (event.getAction()) {
  44. case MotionEvent.ACTION_DOWN:
  45. v.setBackgroundColor(Color.rgb(200, 200, 200));
  46. break;
  47. case MotionEvent.ACTION_UP:
  48. v.setBackgroundColor(Color.WHITE);
  49. break;
  50. }
  51. }


显示对话框:

dialogSelectCall.show()


DialogFragment实现源码:

定义了一个类SelectCallDialog.java继承DialogFragment

  1. package com.bazhangkeji.classroom.common;
  2. import android.app.Dialog;
  3. import android.app.FragmentManager;
  4. import android.graphics.Color;
  5. import android.os.Bundle;
  6. import android.support.annotation.NonNull;
  7. import android.view.Gravity;
  8. import android.view.LayoutInflater;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.widget.Button;
  15. import android.widget.RelativeLayout;
  16. import com.bazhangkeji.classroom.R;
  17. import com.bazhangkeji.classroom.session.VideoSessionActivity;
  18. import com.bazhangkeji.classroom.session.VoiceSessionActivity;
  19. import com.jakewharton.rxbinding2.view.RxView;
  20. import com.trello.rxlifecycle2.android.FragmentEvent;
  21. import com.trello.rxlifecycle2.components.RxDialogFragment;
  22. import java.util.concurrent.TimeUnit;
  23. public class SelectCallDialog extends RxDialogFragment {
  24. private Dialog dialog;
  25. private String userId;
  26. /**
  27. * 初始化.必须调用一次
  28. * @param userId: 目标用户id
  29. */
  30. public void init(String userId) {
  31. this.userId = userId;
  32. }
  33. @NonNull
  34. @Override
  35. public Dialog onCreateDialog(Bundle savedInstanceState) {
  36. dialog = new Dialog(getActivity(), R.style.DialogPopBottom);
  37. View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_select_call, null);
  38. dialog.setContentView(inflate);
  39. dialog.setCanceledOnTouchOutside(true);
  40. Window window = dialog.getWindow();
  41. WindowManager.LayoutParams lp = window.getAttributes();
  42. lp.gravity = Gravity.BOTTOM;
  43. lp.width = WindowManager.LayoutParams.MATCH_PARENT;
  44. window.setAttributes(lp);
  45. RelativeLayout layoutVoice = (RelativeLayout) inflate.findViewById(R.id.layout_voice);
  46. RelativeLayout layoutVideo = (RelativeLayout) inflate.findViewById(R.id.layout_video);
  47. Button buttonCancel = (Button) dialog.findViewById(R.id.cancel);
  48. RxView.clicks(layoutVoice)
  49. .throttleFirst(2, TimeUnit.SECONDS)
  50. .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
  51. .subscribe(v -> {
  52. dialog.cancel();
  53. VoiceSessionActivity.startActivityCallOut(getActivity(), userId);
  54. });
  55. RxView.clicks(layoutVideo)
  56. .throttleFirst(2, TimeUnit.SECONDS)
  57. .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
  58. .subscribe(v -> {
  59. dialog.cancel();
  60. VideoSessionActivity.startActivityCallOut(getActivity(), userId);
  61. });
  62. RxView.clicks(buttonCancel)
  63. .throttleFirst(2, TimeUnit.SECONDS)
  64. .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
  65. .subscribe(v -> dialog.cancel());
  66. RxView.touches(layoutVoice, motionEvent -> {
  67. dealLayoutTouch(layoutVoice, motionEvent);
  68. return false;
  69. }).compose(this.bindUntilEvent(FragmentEvent.DESTROY)).subscribe(motionEvent -> {});
  70. RxView.touches(layoutVideo, motionEvent -> {
  71. dealLayoutTouch(layoutVideo, motionEvent);
  72. return false;
  73. }).compose(this.bindUntilEvent(FragmentEvent.DESTROY)).subscribe(motionEvent -> {});
  74. return dialog;
  75. }
  76. private void dealLayoutTouch(View v, MotionEvent event) {
  77. switch (event.getAction()) {
  78. case MotionEvent.ACTION_DOWN:
  79. v.setBackgroundColor(Color.rgb(200, 200, 200));
  80. break;
  81. case MotionEvent.ACTION_UP:
  82. v.setBackgroundColor(Color.WHITE);
  83. break;
  84. }
  85. }
  86. }

显示对话框前初始化参数:

selectCallDialog.init(userId);

显示对话框:

selectCallDialog.show(getFragmentManager(), "");



参考链接:

1.使用DialogFragment实现底部弹窗布局

2.Android 官方推荐 : DialogFragment 创建对话框

3.详细解读DialogFragment

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号