简体   繁体   中英

How To Make Custom Alert Dialog on Android?

I want to make custom alert dialog with different style. As we know, that the default style is square. I want make it like on candy crush game if you ever seen. I've try to change the background style, but the default background still appear. this is my xml file for my dialog layout.

<?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>

could you help me please? Thanks before...

First, set a custom theme with your background image:

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

Then,set the theme to your dialog:

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

You have specify a background to your AlertDialog's layout. You should make the default windowBackground to disappear. So change android:windowBackground of your dialog to transparent:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM