设置android对话框标题的样式

时间:2012-09-17 20:09:25

标签: android android-dialog android-theme android-styles

我想设置Android对话框的标题样式。这是标准的android对话框看起来像enter image description here

我想要的是像我这样的照片(标题的样式背景和另一种文字颜色)。 enter image description here

我知道可以将主题作为参数传递给Dialog() - 构造函数。但我不知道xml风格的元素会是什么样子。

2 个答案:

答案 0 :(得分:0)

您应该可以使用setCustomTitle(View)。我从来没有用过它,但它应该是这样的:

TextView title = new TextView(context);
title.setBackgroundColor(0xFFFF0000);
title.setTextColor(0xFFFFFFFF);
title.setLayoutParams(/* LayoutParams with MATCH_PARENT width and WRAP_CONTENT height */);
title.setPadding(/* Some padding values */);
yourDialogBuilder.setCustomTitle(title);

就逐字复制现有Android对话框标题的布局而言,我不确定这些值的参数是什么。你可能不得不乱搞和/或谷歌它找到它们。 (字体本身显然是4天以上的Roboto和更少的Droid。)

答案 1 :(得分:0)

非常简单......

您所要做的就是为您上传的图片创建一个XML布局,然后只需用您刚刚创建的XML文件对对话框进行充气......

我只是给你一个示例代码,然后你可以轻松地关注

private View mView;
private Dialog mDialog;
private LayoutInflater mInflater;

现在创建一个函数: -

private void showCustomDialog() {
    mInflater = (LayoutInflater) getBaseContext().getSystemService(
            LAYOUT_INFLATER_SERVICE);
    ContextThemeWrapper mTheme = new ContextThemeWrapper(this,
            R.style.YOUR_STYE);

    mView = mInflater.inflate(R.layout.YOUR_XML_LAYOUT_FILE, null);
    // mDialog = new Dialog(this,0); // context, theme

    mDialog = new Dialog(mTheme);
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    mDialog.setContentView(this.mView);
    mDialog.show();

    TextViiew someText = (TextView) mView.findViewById(R.id.textViewID);
    // do some thing with the text view or any other view 

}

最后确保你的风格为: -

<style name="YOUR_STYLE">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowIsTranslucent">false</item>
    <item name="android:windowContentOverlay">@android:color/transparent</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

那就是......你已经完成了......只要你想要显示自定义对话框就调用这个函数....

希望这个解释很有用......