如何在Android上制作自定义警报对话框?

时间:2014-04-06 11:37:05

标签: android alertdialog

我想制作不同风格的自定义提醒对话框。我们知道,默认样式是正方形。如果你曾经见过,我想让它像糖果粉碎游戏一样。我尝试更改背景样式,但仍会显示默认背景。这是我的对话框布局的xml文件。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:background="@drawable/dialog" >

<ImageView 
    android:id="@+id/ivPic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>
<ScrollView 
    android:id="@+id/svAlert"
    android:layout_width="wrap_content"
    android:layout_below="@+id/ivPic"
    android:layout_height="200px"
    >
    <TextView 
        android:id="@+id/tvMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="text"/>
</ScrollView>
<Button 
    android:id="@+id/bOKE"
    android:gravity="center"
    android:layout_width="wrap_content"
    android:layout_below="@+id/svAlert"
    android:layout_height="wrap_content"
    android:text="OK"/>
</RelativeLayout>
你可以帮我吗? 谢谢你......

2 个答案:

答案 0 :(得分:0)

首先,使用背景图片设置自定义主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyDialogTheme" parent="android:Theme.Dialog">
        <item name="android:windowBackground">@drawable/dialog</item>
    </style>
</resources>

然后,将主题设置为对话框:

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.MyDialogTheme));

答案 1 :(得分:0)

您已指定AlertDialog布局的背景。您应该使默认的windowBackground消失。因此,将对话框的android:windowBackground更改为透明:

<style name="YourDialogTheme" parent="android:Theme.Dialog">
  ...
  <item name="android:windowBackground">@android:color/transparent</item>
</style>