300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android弹出自定义Dialog android自定义Dialog实现底部弹窗

Android弹出自定义Dialog android自定义Dialog实现底部弹窗

时间:2022-12-31 09:27:02

相关推荐

Android弹出自定义Dialog android自定义Dialog实现底部弹窗

android自定义Dialog实现底部弹窗

拿到这个需求,很多人都是直接想用popWindow 实现,但是这样的效果我们完全可以根据系统的Dialog 自定义一个。

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

builder.setTitle("SSS");

builder.show();

这样就直接显示出一个对话框,但是,这个对话框在屏幕的中间,然后,我们去查看dialog的源码:

看dialog.show()里面判断了是否正在显示中,并没有和根布局有关系,看这个类的父类。里面的show()中:

public void show() {

if (mShowing) {

if (mDecor != null) {

if (mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {

mWindow.invalidatePanelMenu(Window.FEATURE_ACTION_BAR);

}

mDecor.setVisibility(View.VISIBLE);

}

return;

}

mCanceled = false;

if (!mCreated) {

dispatchOnCreate(null);

} else {

// Fill the DecorView in on any configuration changes that

// may have occured while it was removed from the WindowManager.

final Configuration config = mContext.getResources().getConfiguration();

mWindow.getDecorView().dispatchConfigurationChanged(config);

}

onStart();

mDecor = mWindow.getDecorView();

if (mActionBar == null && mWindow.hasFeature(Window.FEATURE_ACTION_BAR)) {

final ApplicationInfo info = mContext.getApplicationInfo();

mWindow.setDefaultIcon(info.icon);

mWindow.setDefaultLogo(info.logo);

mActionBar = new WindowDecorActionBar(this);

}

WindowManager.LayoutParams l = mWindow.getAttributes();

if ((l.softInputMode

& WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {

WindowManager.LayoutParams nl = new WindowManager.LayoutParams();

nl.copyFrom(l);

nl.softInputMode |=

WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;

l = nl;

}

mWindowManager.addView(mDecor, l);

mShowing = true;

sendShowMessage();

}

这个调用addView 进去,我们Dialog就是这样加进去的;

可以看到Dialog的源码中的方法,也有onCreate( ) 他也是初始化 对话框的

/**

* Similar to {@link Activity#onCreate}, you should initialize your dialog

* in this method, including calling {@link #setContentView}.

* @param savedInstanceState If this dialog is being reinitialized after a

* the hosting activity was previously shut down, holds the result from

* the most recent call to {@link #onSaveInstanceState}, or null if this

* is the first time.

*/

protected void onCreate(Bundle savedInstanceState) {

}

看Dialog 的实现类。里面有个日期弹窗的实现类,效果如下,更多时间筛选的对话框可以看我的另一个自定义的 文章:

系统的效果如下:

[图片上传失败...(image-ceb45e-1511342382391)]

自定义Dialog实现

1.创建一个自定义类继承dialog类,创建布局,当成Activity 重写onCreate()看效果

public class SmartDialog extends Dialog {

public SmartDialog(@NonNull Context context) {

super(context);

}

public SmartDialog(@NonNull Context context, @StyleRes int themeResId) {

super(context, themeResId);

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.smartlayout);

}

}

布局文件:

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_margin="10dp"

android:layout_height="wrap_content"

android:id="@+id/title"

android:text="自定义风格弹窗"

/>

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

点击事件调用:

SmartDialog smartDialog = new SmartDialog(DialogShowActivity.this);

smartDialog.setTitle("我是Title");

smartDialog.show();

image.png

设置View的 属性,让弹窗到底部

requestWindowFeature(Window.FEATURE_NO_TITLE);//事情定需不需要加

setContentView(R.layout.smartlayout);

WindowManager.LayoutParams params = getWindow().getAttributes();

if(params!=null){

//获取属性可能失败 为空

params.gravity= Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM;

getWindow().setAttributes(params);

}

image.png

更改布局,添加数据

这里我们简单做一个ListView

image.png

.显示效果你会发现没有完全填充,这个时候,再去看我们调用Dialog 默认的构造函数,

查看源码。更改style

public Dialog(@NonNull Context context) {

this(context, 0, true);

}

if (createContextThemeWrapper) {

if (themeResId == 0) {

final TypedValue outValue = new TypedValue();

context.getTheme().resolveAttribute(R.attr.dialogTheme, outValue, true);

themeResId = outValue.resourceId;

}

mContext = new ContextThemeWrapper(context, themeResId);

} else {

mContext = context;

}

如果走默认的构造就不显示,去SDK中找到 R.attr.dialogTheme 先找到自己对应版本的SDK 到自己项目 的build.gradle 中找到

进到自己的安装目录下找对于的 dialogTheme 发现只是定义了 类型,并没具体定义。然后去themes 里面找到:

@style/Theme.Dialog

@layout/dialog_title_icons

@layout/dialog_custom_title

@layout/dialog_title

@dimen/dialog_padding

因为并没有找到具体的定义属性,再去搜索 Theme.Dialog 去 style 中找下 没找到,发现这个文件就在 themes 中,如下:

@null

@style/DialogWindowTitle

@drawable/panel_background

true

@null

@style/Animation.Dialog

stateUnspecified|adjustPan

@bool/config_closeDialogWhenTouchOutside

true

@null

@style/TextAppearance

@style/TextAppearance.Inverse

@color/primary_text_dark

@color/secondary_text_dark

@color/tertiary_text_dark

@color/primary_text_light

@color/secondary_text_light

@color/tertiary_text_light

@color/primary_text_dark_disable_only

@color/primary_text_light_disable_only

@color/primary_text_dark_nodisable

@color/secondary_text_dark_nodisable

@color/primary_text_light_nodisable

@color/secondary_text_light_nodisable

@color/hint_foreground_dark

@color/hint_foreground_light

@color/search_url_text

@style/TextAppearance.Large

@style/TextAppearance.Medium

@style/TextAppearance.Small

@style/TextAppearance.Large.Inverse

@style/TextAppearance.Medium.Inverse

@style/TextAppearance.Small.Inverse

10dip

10dip

10dip

10dip

@style/PreferencePanel.Dialog

我们重点看这个属性: @drawable/panel_background

在资源文件中搜索这个panel_background,打开你会发现是一张 .9图片如下:

image.png

我们自己在项目中实现一个style 去重写这个background 属性,去覆盖

@android:color/white

然后在代码中去使用就直接多传递一个参数去设置:

public Smartdialog(@NonNull Context context) {

super(context,R.style.MtStyle);

}

这样就实现我们要的效果,和PopWindow 一样,但是注意了,在一些新的版本上该效果还是有间隙,这个就要去研究对应SDK的刚才那个属性 中的参数影响了。试了两个版本都不行。。。。只有在老版本上可以,不过也给大家提供了一个思路!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。