弹框如果直接用 Dialog 类, 效果会出现布局方向不可控, 宽度显示全的问题, 或者背景为黑色, 然后就写了一个简单的 BaseDialog 解决这些问题
先上效果图:
giphy.gif
github 代码直通车
BaseDialog 写法, 设置 dialog 窗口宽度为屏幕尺寸, 并设置布局居底部:
- import android.app.Dialog;
- import android.content.Context;
- import android.os.Bundle;
- import android.support.annotation.NonNull;
- import android.support.annotation.StyleRes;
- import android.view.Display;
- import android.view.Gravity;
- import android.view.WindowManager;
- public class BaseDialog extends Dialog{
- public BaseDialog(@NonNull Context context, @StyleRes int themeResId) {
- super(context, themeResId);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 显示在底部
- getWindow().setGravity(Gravity.BOTTOM);
- WindowManager m = getWindow().getWindowManager();
- Display d = m.getDefaultDisplay();
- WindowManager.LayoutParams p = getWindow().getAttributes();
- // 设置 dialog 的宽度为当前手机屏幕的宽度, 默认宽度不是全屏
- p.width = d.getWidth();
- getWindow().setAttributes(p);
- }
- }
继承 BaseDialog 写一个非常简单的 dialog
- public class MenuDialog extends BaseDialog{
- public MenuDialog(@NonNull Context context, @StyleRes int themeResId) {
- super(context, themeResId);
- setContentView(R.layout.create_add_menu);
- }
- }
这里是 create_add_menu.xml 布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical">
- <android.support.v7.widget.CardView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:cardElevation="0dp"
- app:cardCornerRadius="6dp"
- android:layout_margin="10dp"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:android="http://schemas.android.com/apk/res/android">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/roundsmall_bgwhite_top"
- android:orientation="vertical">
- <LinearLayout
- android:id="@+id/ll_upload"
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:orientation="horizontal"
- android:gravity="center_vertical"
- android:padding="15dp">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@mipmap/my_favorites" />
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="wrap_content"
- android:layout_marginLeft="12dp"
- android:textSize="16sp"
- android:textColor="#000"
- android:text="菜单 1"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/ll_compete"
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:orientation="horizontal"
- android:gravity="center_vertical"
- android:padding="15dp">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@mipmap/my_help_and_feedback" />
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="wrap_content"
- android:layout_marginLeft="12dp"
- android:textSize="16sp"
- android:textColor="#000"
- android:text="菜单 2"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/ll_scancode"
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:orientation="horizontal"
- android:gravity="center_vertical"
- android:padding="15dp">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@mipmap/my_historical_record" />
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="wrap_content"
- android:layout_marginLeft="12dp"
- android:textSize="16sp"
- android:textColor="#000"
- android:text="菜单 3"/>
- </LinearLayout>
- <LinearLayout
- android:id="@+id/ll_take_upload"
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:orientation="horizontal"
- android:gravity="center_vertical"
- android:padding="15dp">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@mipmap/my_help_and_feedback" />
- <TextView
- android:layout_width="0dp"
- android:layout_weight="1"
- android:layout_height="wrap_content"
- android:layout_marginLeft="12dp"
- android:textSize="16sp"
- android:textColor="#000"
- android:text="菜单 4"/>
- </LinearLayout>
- </LinearLayout>
- </android.support.v7.widget.CardView>
- </LinearLayout>
点击按钮显示 dialog
- findViewById(R.id.btn_showdialog).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- new MenuDialog(MainActivity.this,R.style.basedialog_style).show();
- }
- });
dialog 公用 style 文件 basedialog_style 代码:
- <style name="basedialog_style" parent="@android:style/Theme.Dialog">
- <item name="android:windowFrame">@null</item>
- <item name="android:windowIsFloating">true</item>
- <item name="android:windowIsTranslucent">true</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:background">@color/total_transparent</item>
- <item name="android:windowBackground">@color/total_transparent</item>
- <item name="android:backgroundDimAmount">0.6</item>
- <item name="android:backgroundDimEnabled">true</item>
- </style>
来源: http://www.jianshu.com/p/b94bc679e360