2016-04-11 7 views
3

は、私は現在、このようになります警告ダイアログがあります。 Current Alert DialogAlertDialogカスタムボタンアンドロイドを作成するには?

をしかし、私は「YES」と「NO」ボタンが背景を持っているか、彼らだけではなく、テキストのボタンがあるように見えるしたいと思います。 AlertDialogのカスタムテーマを実装しようとしましたが、変更できるのは「警告」テキストの色を変更することだけです。

"YES"と "NO"の代わりに実際のボタンが表示されるように、アラートダイアログのテーマをカスタマイズする最も効率的で簡単な方法は何でしょうか?

これが私の現在のres /値/のstyles.xmlファイル

<resources> 

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light"> 
    <!-- Customize your theme here. --> 
    <item name="colorAccent">@color/PCCA_blue</item> 
</style> 

<style name="customAlertDialog" parent="@android:style/Theme.Holo.Dialog"> 
    <item name="android:textColor">@color/red</item> 

</style> 

であり、これは私が構築し、見せるAlertDialogです。何かアドバイスが高く評価され

private void alert(final String action){ 
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.customAlertDialog)); 
    String message = ""; 

    switch(action) { 
     case "upload": 
      message = "Are you sure you want to upload the counted quantities?\n\nIt may take a few minutes to reflect your changes."; 
      break; 
     case "download": 
      message = "Downloading new worksheets may overwrite existing counted quantities, do you want to continue?\n\n" + 
        "NOTE: You may want to upload counts first.\n\n" + 
        "(Changed counted quantities will still be available for upload)"; 
      break; 
     default: 
      message = action; 
      break; 
    } 


    alertDialogBuilder.setTitle("WARNING"); 
    alertDialogBuilder.setMessage(message); 
    DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      if(which == DialogInterface.BUTTON_POSITIVE) { 
       if (action.equals("upload")) { 
        MainActivity.upload(context); 
       } else if (action.equals("download")) { 
        MainActivity.download(context); 
       } 
      } else { 
       dialog.cancel(); 
      } 
     } 
    }; 

    alertDialogBuilder.setPositiveButton("YES", listener); 
    alertDialogBuilder.setNegativeButton("NO", listener); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 

} 

。私はアンドロイドに新しいです。

+1

独自のレイアウトを作成し、それをダイアログのビューとして設定することができます。それはもう少し作業とセットアップが必要ですが、それを行う方法を知っている唯一の方法です。 – zgc7009

+0

'setView()'は、ボタンではなく、ダイアログ本体のためのものです。 –

答えて

1

AlertDialogはgetButton()メソッドを持っています。あなたはボタンを手に入れて背景を設定することができます。いくつかの注意点があります:あなたはダイアログが表示された後でしかそのメソッドを呼び出すことができません(私はonStart()がDialogクラスで呼び出された後に信じています)。

これは、パディング、余白、レイアウトなどを考慮した外観を得られない可能性が高いことを意味します。AlertDialogは、システムテーマに一致する標準的な見た目のダイアログを持つことです。それは、カスタマイズ可能であることを意図したものではなく、UIに賢明です。

それ以外の場合は、完全にカスタムのダイアログを作成する必要があります。 DialogFragment、カスタムダイアログ、ダイアログテーマアクティビティなど、いくつかのオプションがあります。詳細については、Googleのトピックをご覧ください。

関連する問題