江西老表 發表於 2020-12-18 12:01:00

Android开发 自定义Dialog(BottomSheetDialog与Dialog)开发记录

<h1><span style="color: rgba(0, 128, 128, 1)">前言</span></h1>
<p>  记录一些自己项目上自定义的Dialog</p>
<h1><span style="color: rgba(0, 128, 128, 1)">泛用性基础Dialog</span></h1>
<pre class="highlighter-hljs" data-dark-theme="true"><code>
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class BaseDialog extends Dialog {
    private float widthRatio = 1f;
    private float heightRatio = 1f;
    private boolean mIsShowNavigation = false;

    public BaseDialog(@NonNull Context context) {
      super(context);
    }

    public BaseDialog(@NonNull Context context, int themeResId) {
      super(context, themeResId);
    }

    protected BaseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
      super(context, cancelable, cancelListener);
    }

    /**
   * 设置对话框宽度与屏幕宽度占比 0.0~1.0
   *
   * @param widthRatio
   */
    public void setWidthHeightRatio(float widthRatio, float heightRation) {
      this.widthRatio = widthRatio;
      this.heightRatio = heightRation;
    }

    public void setShowNavigation(boolean isShowNavigation) {
      mIsShowNavigation = isShowNavigation;
    }

    @Override
    protected void onStart() {
      super.onStart();
      //隐藏导航栏
      if (!mIsShowNavigation) {
            WindowManager.LayoutParams params = getWindow().getAttributes();
            params.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                  | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                  | View.SYSTEM_UI_FLAG_IMMERSIVE
                  | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
            getWindow().setAttributes(params);
      }

      //去除对话框内边距与背景透明
      WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
      if (widthRatio == 1) {
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
      } else {
            float width = getContext().getResources().getDisplayMetrics().widthPixels * widthRatio;
            layoutParams.width = (int) width;
      }
      if (heightRatio == 1) {
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
      } else {
            float height = getContext().getResources().getDisplayMetrics().heightPixels * heightRatio;
            layoutParams.height = (int) height;
      }
      getWindow().setAttributes(layoutParams);
      getWindow().getDecorView().setBackground(new ColorDrawable(Color.TRANSPARENT));
      getWindow().getDecorView().setPadding(0, 0, 0, 0);

    }
}</code></pre>
<h1><span style="color: rgba(0, 128, 128, 1)">实现对话框全透明</span></h1>
<p>在styles添加</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">style </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="dialogTransparent"</span><span style="color: rgba(255, 0, 0, 1)"> parent</span><span style="color: rgba(0, 0, 255, 1)">="@android:style/Theme.Dialog"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:windowFrame"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>@null<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">边框</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:windowIsFloating"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>true<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">是否浮现在activity之上</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:windowIsTranslucent"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>true<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">半透明</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:windowNoTitle"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>true<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">无标题</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:background"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>@android:color/transparent<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">背景透明</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:windowBackground"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>@android:color/transparent<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">背景透明</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:backgroundDimEnabled"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>true<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">模糊</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
      <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">item </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="android:backgroundDimAmount"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>0.5<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">item</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span> <span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)">背景透明度0到1 50%就是0.5</span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">style</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>将风格添加</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> NumberInputDialog <span style="color: rgba(0, 0, 255, 1)">extends</span> BottomSheetDialog <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> View.OnClickListener {

    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> NumberInputDialog(@NonNull Context context) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(context,R.style.dialogTransparent);
      initView();
    }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">略...</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">设置背景透明</span></h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      getWindow().getDecorView().setBackground(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> ColorDrawable(Color.TRANSPARENT));
}</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">设置全屏</span></h1>
<p>请注意,在开发的时候经常会发现,我们设置的全屏的宽度或者全屏的高度后经常外面还要一些边缘空白空间,需要设置setPadding来取消空白内边距,代码如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      getWindow().getDecorView().setPadding(</span>0,0,0,0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取消内边距空白</span>
      getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置全屏</span>
}</pre>
</div>
<p>或者</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      getWindow().getDecorView().setPadding(</span>0,0,0,0);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">取消内边距空白</span>
      WindowManager.LayoutParams layoutParams =<span style="color: rgba(0, 0, 0, 1)"> getWindow().getAttributes();
      layoutParams.width </span>=<span style="color: rgba(0, 0, 0, 1)"> WindowManager.LayoutParams.MATCH_PARENT;
      layoutParams.height </span>=<span style="color: rgba(0, 0, 0, 1)"> WindowManager.LayoutParams.MATCH_PARENT;
      getWindow().setAttributes(layoutParams);
}</span></pre>
</div>
<p>在取消内边距空白后,还有一个好处,那就是设置&nbsp;layoutParams.width 与layoutParams.height 会更加准确</p>
<h1><span style="color: rgba(0, 128, 128, 1)">设置对话框显示位置</span></h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置显示位置Gravity.BOTTOM Gravity.TOPGravity.LEFTGravity.RIGHTGravity.CENTER</span>
<span style="color: rgba(0, 0, 0, 1)">      getWindow().setGravity(Gravity.BOTTOM);
}</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">弹出输入法并且焦点选中输入框</span></h1>
<div class="cnblogs_code">
<pre>      EditText editTextTextPersonName =<span style="color: rgba(0, 0, 0, 1)"> findViewById(R.id.editTextTextPersonName);
      getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置输入盘可见</span>
      editTextTextPersonName.requestFocus();</pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">设置背景阴影透明度</span></h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onCreate(Bundle savedInstanceState) {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onCreate(savedInstanceState);
      WindowManager.LayoutParams layoutParams </span>=<span style="color: rgba(0, 0, 0, 1)"> getWindow().getAttributes();
      layoutParams.dimAmount </span>= 0f;<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调整透明度</span>
<span style="color: rgba(0, 0, 0, 1)">      getWindow().setAttributes(layoutParams);
    }</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">BottomSheetDialog禁用下滑退出</span></h1>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> fun showDurationPickerDialog(){
      val dialog </span>= context?<span style="color: rgba(0, 0, 0, 1)">.let { BottomSheetDialog(it) }
      val binding </span>=<span style="color: rgba(0, 0, 0, 1)"> DialogDurationPickerBinding.inflate(layoutInflater)
      dialog</span>?<span style="color: rgba(0, 0, 0, 1)">.setContentView(binding.root)
      binding.root.post {
</span><span style="color: rgba(0, 0, 0, 1)">            val view </span>= dialog?.findViewById&lt;View&gt;<span style="color: rgba(0, 0, 0, 1)">(R.id.design_bottom_sheet)
            val behavior </span>= view?.let { BottomSheetBehavior.<span style="color: rgba(0, 0, 255, 1)">from</span><span style="color: rgba(0, 0, 0, 1)">(it) }
            behavior</span>?.isHideable = <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
      }
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">略...</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">BottomSheetDialog背景透明(显示圆角)</span></h1>
<p>BottomSheetDialog对话框其实是自带一个白色背景的,我们在自己的布局设置圆角shape后会出现不显示的情况.</p>
<div class="cnblogs_code">
<pre>      val dialog = BottomSheetDialog(<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">)
      dialog.setContentView(R.layout.main_dialog_bottom_wheel)
      dialog.show()
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">设置背景透明,需要在show方法后面</span>
      dialog.findViewById&lt;View&gt;(R.id.design_bottom_sheet)?.setBackgroundResource(android.R.color.transparent)</pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">横屏显示不全的问题解决</span></h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onStart() {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onStart();
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (mIsHorizontal){
            getBehavior().setState(BottomSheetBehavior.STATE_EXPANDED);
      }
    }</span></pre>
</div>
<h1><span style="color: rgba(0, 128, 128, 1)">隐藏状态栏与导航栏</span></h1>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">    @Override
    </span><span style="color: rgba(0, 0, 255, 1)">protected</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> onStart() {
      </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">.onStart();
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">略...
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">隐藏导航栏</span>
      WindowManager.LayoutParams params =<span style="color: rgba(0, 0, 0, 1)"> getWindow().getAttributes();
      params.systemUiVisibility </span>= View.SYSTEM_UI_FLAG_FULLSCREEN |<span style="color: rgba(0, 0, 0, 1)"> View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                </span>|<span style="color: rgba(0, 0, 0, 1)"> View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                </span>|<span style="color: rgba(0, 0, 0, 1)"> View.SYSTEM_UI_FLAG_IMMERSIVE
                </span>|<span style="color: rgba(0, 0, 0, 1)"> View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
      getWindow().setAttributes(params);
    }</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>end</p>

</div>
<div id="MySignature" role="contentinfo">
    <div style="text-align: center">
    <p style="color:orange;font-size:16px;" >本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/14154232.html </p>
    <div style="color:orange;font-size:16px;">本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。 </div>
</div><br><br>
来源:https://www.cnblogs.com/guanxinjing/p/14154232.html
頁: [1]
查看完整版本: Android开发 自定义Dialog(BottomSheetDialog与Dialog)开发记录