gpt4 book ai didi

android - 设置扩展 Dialog 的 CustomDialog 主题的最佳实践是什么 OnClickListener

转载 作者:行者123 更新时间:2023-11-29 22:15:17 24 4
gpt4 key购买 nike

我目前在我的申请中遇到了一个大问题。我在整个应用程序中使用 Theme.Light。结果很好,但我注意到在 ICS 中,对话框的背景是白色的,而在以前的版本中,背景是黑色的(见下面的截图)

我的第一个想法是对项目使用不同的布局,并使用带有深色文本的 layout-v14 和带有白色文本的布局文件夹。

这会起作用,但我真的很想像设置经典对话框一样设置对话框的主题:

return new AlertDialog.Builder(AlertDialogSamples.this, AlertDialog.THEME_TRADITIONAL)
.create();

现在的问题是,我的自定义 Dialog 扩展了 Dialog,我再也无法使用构建器了:

public class MyCustomDialog extends Dialog implements OnClickListener {

int THEME=AlertDialog.THEME_TRADITIONAL;

public MyCustomDialog(Activity plannerActivity) {
super(plannerActivity);
View myView=//Something;
setContentView(myView);
}
}

在我的主要代码中:

new MyCustomDialog(getActivity()).show();

由于我无法再访问构建器,我无法更改 CustomDialog 的主题,我完全陷入了我的应用程序。

非常感谢您的帮助!

enter image description here

编辑以添加更多信息

根据 Vineet Shukla 的建议,我尝试了 new DialogYour(Activity.this, android.R.style.Theme_Translucent_NoTitleBar),但构造函数 DialogYour(Activity, int) 未定义。

我不能用主题覆盖 DialogYour,因为有些东西是 android 内部的!

当我查看对话代码时:

Dialog(Context context, int theme, boolean createContextWrapper) {
if (theme == 0) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.dialogTheme,
outValue, true);
theme = outValue.resourceId;
}

mContext = createContextWrapper ? new ContextThemeWrapper(context, theme) : context;
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Window w = PolicyManager.makeNewWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mUiThread = Thread.currentThread();
mListenersHandler = new ListenersHandler(this);
}

我不能覆盖,因为 PolicyManager 属于 com.android.internal

最佳答案

我尝试过自定义对话框,但不是这种方式。我在 activityoverridden onCreateDialog 主题 android.R.style.Theme_Translucent_NoTitleBar

protected Dialog onCreateDialog(int id) {
Dialog dialog = new DialogYour(Activity.this, android.R.style.Theme_Translucent_NoTitleBar);
if(id == YOUR_ID){
dialog.setContentView(R.layout.layout);
//initialize your views here ...
}

return dialog;
}

您可以尝试这种方式,您将获得您想要的自定义布局布局。

注意:您也可以尝试在代码中使用主题 android.R.style.Theme_Translucent_NoTitleBar

编辑1:

我还没有尝试过,但是要根据您的情况设置主题,您可以这样尝试:

ConnectionDialog dialog = new ConnectionDialog(getActivity()); 
dialog.getWindow().getAttributes().windowAnimations = android.R.style.Theme_Translucent_NoTitleBar;
dialog.show();

Edit2:我已经测试了您的代码,问题出在您的构造函数中。

public class MyCustomDialog extends Dialog implements OnClickListener {

int THEME=AlertDialog.THEME_TRADITIONAL;

public MyCustomDialog(Context context, int theme) {
super(context, theme);
View myView=//Something;
setContentView(myView);
}
}

现在像这样调用上面的对话框:

 MyCustomDialog dialog = new MyCustomDialog (this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.show();

关于android - 设置扩展 Dialog 的 CustomDialog 主题的最佳实践是什么 OnClickListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8642306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com