回答
刚刚发现这篇文章,同时试图找出如何做同样的事情。以下是我为未来发现这一点的其他人做过的事情。
风格的XML如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PauseDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowTitleStyle">@style/PauseDialogTitle</item>
</style>
<style name="PauseDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
<item name="android:gravity">center_horizontal</item>
</style>
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">@android:style/TextAppearance.DialogWindowTitle</item>
</style>
</resources>
在我的活动onCreateDialog因为我想要的对话框方法风格创建对话框是这样的:
Dialog pauseDialog = new Dialog(this, R.style.PauseDialog);
pauseDialog.setTitle(R.string.pause_menu_label);
pauseDialog.setContentView(R.layout.pause_menu);
你已经有了一些启动提示这里修改对话框的标题: Android - change custom title view at run time 不知道它是否可以居中(没试过),但如果它是一个自定义视图我想这是非常有可能的。
如果您没有拨打AlertDialog.Builder.setIcon()
和AlertDialog.Builder.setTitle()
,那么您的自定义对话框将不会显示内置/默认标题View。在这种情况下,您可以添加自定义标题视图:
AlertDialog.Builder.setView(View view)
只要它是你谁创建这个视图中,可以实现任何类型的对齐。
这里是一个讨厌的解决方案...扩展AlertDialog.Builder并覆盖所有的方法(例如,setText,setTitle,setView等),以便不设置实际的Dialog文本/标题/视图,而是在Dialog的View中创建一个新的视图。那么你可以随心所欲地设计任何样式。
为了阐明,就父级而言,设置了视图,没有别的。
就您的自定义扩展类而言,一切都在该视图内完成。
的另一种方式,这可以通过编程使用setCustomTitle()来完成:
// Creating the AlertDialog with a custom xml layout (you can still use the default Android version)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.viewname, null);
builder.setView(view);
TextView title = new TextView(this);
// You Can Customise your Title here
title.setText("Custom Centered Title");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
builder.setCustomTitle(title);
'R.layout.viewname'在此布局中必须包含哪些内容?只有'TextView'? – DroidLearner 2012-12-22 03:28:20
整个视图部分不需要包含,只有当您想要自定义AlertDialog的默认视图时才需要包含整个视图部分。我确信它必须是一个TextView。 – 2012-12-23 13:55:38
我得到这个textview的灰色边框。我如何删除它?我试过\t \t \t title.setBackgroundResource(R.drawable.black_border);但是这没有帮助。 @LandLPartners – user1051505 2013-12-07 07:29:46
你可以做到这一点的代码。假设你有对话框片段,然后添加下面的代码行。
@Override
public void onStart()
{
super.onStart();
TextView textView = (TextView) this.getDialog().findViewById(android.R.id.title);
if(textView != null)
{
textView.setGravity(Gravity.CENTER);
}
}
为您定制DialogFragment你可以这样做:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = super.onCreateDialog(savedInstanceState);
final TextView textView = (TextView) dialog.findViewById(android.R.id.title);
if(textView != null) {
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
return dialog;
}
这对我不起作用。 findViewById(android.R.id.title);返回null。 – Alyoshak 2016-11-28 22:31:47
TextView titleView = (TextView) dialog.findViewById(android.R.id.title);
if(titleView != null) {
titleView.setGravity(Gravity.CENTER);
}
详情请参阅this KodeCenter article on Android Dialog and AlertDialog。(使用android.support.v7.app.AlertDialog
)
TextView titleText = (TextView) helpDialog.findViewById(R.id.alertTitle);
if(titleText != null) {
titleText.setGravity(Gravity.CENTER);
}
的完整代码:
试试这个
AlertDialog.Builder helpDialogBuilder = new AlertDialog.Builder(context)
.setTitle(/*your title*/)
.setMessage(/*your message*/)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/*you can do something here*/
dialog.dismiss();
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
/*you can do something here*/
dialog.dismiss();
}
});
final AlertDialog helpDialog = helpDialogBuilder.create();
helpDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
TextView titleText = (TextView) helpDialog.findViewById(R.id.alertTitle); if(titleText != null) { titleText.setGravity(Gravity.CENTER); }
TextView messageText = (TextView) helpDialog.findViewById(android.R.id.message);
if(messageText != null) {
messageText.setGravity(Gravity.CENTER);
}
}
});
helpDialog.show();
您可以通过编程做没有自定义视图:
@Override
public void onStart()
{
super.onStart();
TextView textViewVanilla = (TextView) this.getDialog().findViewById(android.R.id.title);
if(textViewVanilla != null)
{
textViewVanilla.setGravity(Gravity.CENTER);
}
// support for appcompat v7
TextView textViewAppcompat = (TextView) this.getDialog().findViewById(android.support.v7.appcompat.R.id.alertTitle);
if(textViewAppcompat != null)
{
textViewAppcompat.setGravity(Gravity.CENTER);
}
}
感谢@hesam为理念。对于appcompat布局,请参见Android/sdk/platforms/android-26/data/res/layout/alert_dialog_title_material.xml
- 1. Android对话框标题框我怎样才能使它更小
- 2. 我怎样才能让自定义对话框angeled边缘
- 3. 我怎样才能让我的提醒对话在Android的自定义提醒
- 4. 我怎样才能让AngularJS绑定一个fancybox对话框?
- 5. Android AlertDialog我怎样才能识别出对话框的对象
- 6. 我怎样才能使对话框动态定时器在android
- 7. 怎样才能让android中
- 8. 我怎样才能让网站链接在警告对话框
- 9. 我怎样才能让自定义导航栏透明的RNRF
- 10. 我怎样才能让它干?
- 11. 如何在Android上自定义对话框标题的中心
- 12. Android的对话框自定义样式
- 13. 我怎样才能让
- 14. 我怎样才能让这样的
- 15. AdMob在自定义对话框中?怎么样? Android
- 16. 我怎样才能在Android 4.0的上
- 17. 自定义警报对话框不在Android上垂直居中
- 18. WPF - 我怎样才能使一个自定义边框
- 19. 我怎样才能让我的自定义指令的NG-重复
- 20. 我怎样才能让ReSharper的让我标签其他
- 21. 我怎样才能为styleBar的UINavigationBar创建自定义样式?
- 22. 我怎样才能把日期和时间选择在对话框中的Android
- 23. 我怎样才能在ngDialog中设置对话框宽度
- 24. Mongoid:我怎样才能让Mongoid识别我的自定义ActiveModel验证?
- 25. 我怎样才能让我的.apk签名在android中发布
- 26. 我该怎么做才能让它自动打电话?
- 27. 我怎样才能在标题高图
- 28. 我怎样才能使用我的自定义下拉多次
- 29. 我怎样才能让我的chatbot谈话第一?
- 30. Magento菜单:我怎样才能让它左对齐?
我试过了你的建议,但它没有将对话框的标题居中(HTC Wildfire与Android 2.2.1一起运行)......任何想法?因为直观上你的解决方案很有意义:) – Ready4Android 2011-08-28 23:47:42
对不起,不知道。我已经在我的Ideos和1.6和2.2的模拟器上测试过了,没有任何问题。 – ChrisJD 2011-10-16 00:28:08
没有“parent =”@ android:style/DialogWindowTitle“”。 – Hesam 2011-11-30 10:35:31