簡體   English   中英

Dialog Activity 在背景上有一個布局

[英]Dialog Activity has a layout on the background

單擊通知時,我正在嘗試進行對話活動,但遇到了一些麻煩。

在此處輸入圖像描述

如您所見,單擊通知后會打開對話框,但不需要黑色背景,我該如何刪除它? 我想做的只是在恢復到后台活動之前彈出應用程序上的對話框。 謝謝。

這是我的清單代碼。

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity> <!-- android:parentActivityName=".MainActivity" -->
        <activity
            android:name=".DialogMEssage"
            android:excludeFromRecents="true"
            android:launchMode="singleInstance"
            android:taskAffinity=""
            android:theme="@style/Theme.AppCompat.Dialog.Alert" >
        </activity>
    </application>

布局文件完全為空,對話框的代碼如下。

public class DialogMEssage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_m_essage);
        
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }
}

從您的代碼中刪除此行

setContentView(R.layout.activity_dialog_m_essage);

它不會設置該背景布局

添加DialogMEssage.this.finish(); 在 onClick 像這樣

AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage("Alert message to be shown");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        DialogMEssage.this.finish();
                    }
                });
        alertDialog.show();

您可以嘗試以下方法。

1 - 添加以下樣式:

<style name="Theme.AppCompat.Dialog.Alert.Custom" parent="Theme.AppCompat.Dialog.Alert">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

2 - Go 到清單並更新您的活動主題:

<activity
    android:name=".DialogMEssage"
    android:excludeFromRecents="true"
    android:launchMode="singleInstance"
    android:taskAffinity=""
    android:theme="@style/Theme.AppCompat.Dialog.Alert.Custom" >
</activity>

3 - (可選)您可以在用戶關閉對話框后關閉活動:

alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        // Close the activity
        finish();
    }
});

暫無
暫無

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

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