簡體   English   中英

如何在Android上制作自定義提醒對話框?

[英]How To Make Custom Alert Dialog on Android?

我想用不同的樣式制作自定義警報對話框。 眾所周知,默認樣式是正方形。 我想讓它像在糖果迷戀游戲中一樣,如果您曾經見過。 我嘗試更改背景樣式,但是默認背景仍然出現。 這是我的對話框布局的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>

請問你能幫幫我嗎? 謝謝...

首先,使用背景圖片設置自定義主題:

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

您已經為AlertDialog的布局指定了背景。 您應該使默認的windowBackground消失。 因此,將對話框的android:windowBackground更改為透明:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM