Android - 更改自定义对话框标题背景
我正在创建一个自定义对话框,我想知道如何更改标题栏的背景。
我尝试了两种方法:
1 - 我尝试了 AlertDialog.Builder 方法“setCustomTitle”。我创建了一个简单的布局视图,其中包含布局宽度和高度“match_parent”以及背景颜色的文本视图。当我运行应用程序时,只有标题栏的上半部分显示背景颜色。下半部分仍然显示默认的主题背景颜色。有谁知道为什么?
2 - 我创建了自己的对话框主题。我创建了一个具有父继承“@android:style/Theme.Holo.Light.Dialog”的样式。然后我将其传递到 AlertDialog.Builder 构造函数中 - new AlertDialog.Builder(this, R.style.test_dialog)。看起来不错,但不知何故,对话框被包装在对话框中。对话框周围有一个方框。 有谁知道为什么?
I am creating a custom dialog and I want to know how to change the background of the title bar.
I've tried two approaches:
1 - I've tried the AlertDialog.Builder method 'setCustomTitle'. I created a simple layout view comprising of a textview with layout width and height 'match_parent' and background color. When I run the app, only the top half of the title bar is showing the background color. The bottom half is still showing the default theme background color. Does anyone know why?
2 - I've created my own dialog theme. Ive created a style with parent inheritance to '@android:style/Theme.Holo.Light.Dialog'. I've then passed that in the AlertDialog.Builder constructor - new AlertDialog.Builder(this, R.style.test_dialog). It seems good but somehow the dialog is wrapped within a dialog. A square box is surrounding the dialog.
Does anyone know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以创建类似的样式,
并且可以实例化对话框:
现在对话框显示为黑色标题背景色。
You can create a style like,
And you can instantiate dialog:
Now the dialog shows up with black title background color.
对话框内对话框的外观是由对话框的窗口背景引起的。每个对话框都有这个,但默认的 Android 对话框将窗口背景设置为透明。为此,请将此项目添加到您的自定义对话框主题中:
The dialog-wrapped-within-a-dialog appearance is caused by the dialog's window background. Every dialog has this, but the default Android dialogs have the window background set to transparent. To do this, add this item in your custom dialog theme:
我正在使用 Mono Android(Xamarin);我向您展示在我的应用程序中用于在片段中创建对话框的另一种替代方案:
Resource.Layout.listitem_custom_dialog:这是自定义列表视图布局,这里是 xml 文件:
Android.Resource.Id.Title:这是包含对话框标题的文本视图的 ID。它是由 android 预定义的。
这样您将获得一个可以按照您想要的方式设置样式的对话框。
I am using Mono Android(Xamarin); am showing you another alternative that am using in my app to create a dialog in a fragment:
Resource.Layout.listitem_custom_dialog: this is custom listview layout, here is the xml file:
Android.Resource.Id.Title: this is the id of the textview containing the dialog title. and it is predefined by android.
this way you will get a dialog that you can style in way you want.
为您的标题创建一个 xml 布局文件并将其展开并设置为 AlertDialog 作为
Create an xml layout file for your title and inflate it and set to to the AlertDialog as
您可以像这样设置自定义标题
,并且在 custom_title 布局中您可以创建这样的自定义标题
You can just set custom title like this
and in custom_title layout you can create custom title like this