2016-11-07 108 views
0

enter image description here顯示自定義佈局警告對話框

我開發一個應用程序中,我必須表明它在屏幕截圖中顯示在屏幕上的按鈕唯一的按鈕。但是我想在其他一些活動上顯示這個按鈕。 例如我有一個在屏幕上的活動。現在兩分鐘後,我想顯示一個自定義提示對話框,即屏幕截圖中顯示的這個屏幕,但不是隻顯示按鈕,而是提示對話框將整個屏幕顯示爲一個對話框,即白色背景。我如何擺脫這種背景,只有這個按鈕可以作爲一個警告對話框顯示在屏幕上? 我這個自定義佈局代碼:警報對話框

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<Button 
    android:text="Unable to Snooze" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="15dp" 
    android:id="@+id/button" 
    android:background="#088a68" 
    android:textColor="#FFFFFF" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="15dp" /> 

和活動代碼:

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 

      AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(ActivityA.this); 
      LayoutInflater inflater = ActivityA.this.getLayoutInflater(); 
      View dialogView = inflater.inflate(R.layout.alert, null); 
      dialogBuilder.setView(dialogView); 

      AlertDialog alert11 = dialogBuilder.create(); 
      alert11.show(); 

     } 
    }, 120000); 

注意我要顯示在屏幕底部的按鈕作爲alertdialog

回答

1

編輯:

使用對話框代替AlertDialog.Builder,因此使用setContentView代替setView。並檢查這一個:Dialog with transparent background in Android

老答案:

如果你是要告訴在多個活動這個按鈕,你最好使用DialogFragment。此外,您可以將片段背景設置爲透明,並且可以調整對話框的大小以適合按鈕。

如何使用對話片段:https://stackoverflow.com/a/20405684/1781718

+0

小號我只想在一項活動中展示它。你可以編輯你的答案嗎? –

+0

請檢查編輯答案。 – dcanbatman

+0

好吧,我要去試試看,謝謝 –

0

嘗試用:

AlertDialog alert11 = dialogBuilder.create(); 
// Add this 
alert11.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

alert11.show(); 
+0

好吧,先生讓我試試這個 –

+0

不,它不工作:( –

1

試試這個: -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <Button 
     android:id="@+id/btn_snooze" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Unable to snooze"/> 
</LinearLayout> 

&調用此方法可在任何需要

public void showDialog() { 

     dialog = new Dialog(MainActivity.this); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.getWindow().setBackgroundDrawable(
       new ColorDrawable(android.graphics.Color.TRANSPARENT)); 
     dialog.setContentView(R.layout.dialog_layout); 
     dialog.setCanceledOnTouchOutside(false); 
     dialog.setCancelable(true); 

     btn_snooze = (Button) dialog.findViewById(R.id.btn_snooze); 

     btn_snooze.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       dialog.cancel(); 
      } 
     }); 

     dialog.show(); 
    } 
+0

問題已解決,但你的答案upvote :) –