2016-06-20 40 views
0

AlertDialogボタンのアクセントの色をカスタマイズしようとしています。しかし、システムから色を継承しているように見えても影響はありません。ここに私のスタイル/テーマがあります。AlertDialogのカスタムテーマが機能しない

<color name="actionable_items">#0574ac</color> <!-- it is blue color --> 
    <style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert"> 
     <!--buttons color--> 
     <item name="colorAccent">@color/actionable_items</item> 
     <!--item RadioButton or CheckBox color--> 
     <item name="colorControlActivated">@color/actionable_items</item> 
     <item name="colorPrimary">@color/actionable_items</item> 
     <item name="colorPrimaryDark">@color/actionable_items</item> 
     <item name="android:listChoiceIndicatorMultiple">@color/actionable_items</item> 
     <item name="android:listChoiceIndicatorSingle">@color/actionable_items</item> 
    </style> 

ここでは、Alertdialogを構築しようとしている私のコードです。

final CustomPopupBuilder removePlaceDialog = new CustomPopupBuilder(new ContextThemeWrapper(context, 
                  R.style.LLDialog)); 
      removePlaceDialog.setTitle(getString(R.string.delete_place, placeName)); 
      removePlaceDialog.setMessage(getString(R.string.delete_place_message)); 
      removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() { 

       public void onClick(DialogInterface dialog, int which) { 
        .... 
        .... 
       } 
      }); 
      removePlaceDialog.setNegativeButton(R.string.cancel, null); 
      removePlaceDialog.create().show(); 

最後のAlertDialogには、同じテキストカラーのボタンがありません。テキストの色は緑に似ています。カスタマイズされたテーマではなく、システムから色を継承しているようです。 enter image description here

EDIT1: はここに画像である

私が使用AlertDialog.Builderをしようとしたが、それは私に同じ結果を与えます。

final AlertDialog.Builder removePlaceDialog = AlertDialog.Builder(new ContextThemeWrapper(context, 
                  R.style.LLDialog)); 
       removePlaceDialog.setTitle(getString(R.string.delete_place, placeName)); 
       removePlaceDialog.setMessage(getString(R.string.delete_place_message)); 
       removePlaceDialog.setPositiveButton(R.string.ok_button, new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 
         .... 
         .... 
        } 
       }); 
       removePlaceDialog.setNegativeButton(R.string.cancel, null); 
       removePlaceDialog.create().show(); 

EDIT2:

私はまた、ダイアログボックスのアクセントカラーを変更しようとしましたが、私はその色が表示されない:

<style name="LLDialog" parent="Theme.AppCompat.Light.Dialog.Alert"> 
     <!--buttons color--> 
     <item name="colorAccent">#990000</item> 
     ... 
     ... 
</style> 

であっても、これは変更されません。ボタンのテキストの色:(。

+0

代わりに 'CustomPopupBuilder'を使用する理由は、デフォルトの' AlertDialog'を使うことができます.. !! –

+0

@ jankigadhiya私はAlertDialogを使用しましたが、同じ結果です。それは私のテーマ/スタイルに従ってalertDialogをカスタマイズしません。 – Rakesh

答えて

0

ここでは、それがあなたを助けることを願って

public class CustomDialogUI { 
    Dialog dialog; 
    Vibrator vib; 
    RelativeLayout rl; 

    @SuppressWarnings("static-access") 
    public void dialog(final Context context, String title, String message, 
      final Runnable task) { 
     dialog = new Dialog(context); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.custom); 
     dialog.setCancelable(false); 
     TextView m = (TextView) dialog.findViewById(R.id.message); 
     TextView t = (TextView) dialog.findViewById(R.id.title); 
     final Button n = (Button) dialog.findViewById(R.id.button2); 
     final Button p = (Button) dialog.findViewById(R.id.next_button); 
     rl = (RelativeLayout) dialog.findViewById(R.id.rlmain); 
     t.setText(bold(title)); 
     m.setText(message); 
     dialog.show(); 
     n.setText(bold("Close")); 
     p.setText(bold("Ok")); 
     // color(context,rl); 
     vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); 
     n.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       vib.vibrate(15); 
       dialog.dismiss(); 
      } 
     }); 
     p.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       vib.vibrate(20); 
       dialog.dismiss(); 
       task.run(); 
      } 
     }); 
    } 
    //customize text style bold italic.... 
    public SpannableString bold(String s) { 
     SpannableString spanString = new SpannableString(s); 
     spanString.setSpan(new StyleSpan(Typeface.BOLD), 0, 
       spanString.length(), 0); 
     spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0); 
     // spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0, 
     // spanString.length(), 0); 
     return spanString; 
    } 

    } 

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="match_parent" 
android:background="#00000000" 
> 

<RelativeLayout 
    android:id="@+id/rlmain" 
    android:layout_width="fill_parent" 
    android:layout_height="150dip" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:background="#569CE3" > 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="25dip" 
     android:layout_marginTop="10dip" > 

     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="Are you Sure?" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#ffffff" 
      android:textSize="13dip" /> 
    </RelativeLayout> 

    <RelativeLayout 
     android:id="@+id/relativeLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/relativeLayout1" 
     android:layout_alignRight="@+id/relativeLayout1" 
     android:layout_below="@+id/relativeLayout1" 
     android:layout_marginTop="5dip" > 
    </RelativeLayout> 

    <ProgressBar 
     android:id="@+id/process" 
     style="?android:attr/progressBarStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="3dip" 
     android:layout_marginTop="3dip" /> 

    <RelativeLayout 
     android:id="@+id/relativeLayout3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/relativeLayout2" 
     android:layout_below="@+id/relativeLayout2" 
     android:layout_toLeftOf="@+id/process" > 

     <TextView 
      android:id="@+id/message" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textColor="#ffffff" 
      android:textSize="13dip"/> 

    </RelativeLayout> 

    <Button 
     android:id="@+id/next_button" 
     android:layout_width="90dip" 
     android:layout_height="35dip" 
     android:layout_alignParentBottom="true" 
     android:textColor="@drawable/button_text_color" 
     android:background="@drawable/blue_button" 
     android:layout_marginBottom="5dp" 
      android:textSize="10dp" 

     android:layout_alignRight="@+id/relativeLayout3" 
     android:text="Okay" /> 

    <Button 
     android:id="@+id/button2" 
     android:text="Cancel" 
     android:textColor="@drawable/button_text_color" 
     android:layout_width="90dip" 
     android:layout_height="35dip" 
     android:layout_marginBottom="5dp" 
     android:background="@drawable/blue_button" 
     android:layout_marginRight="7dp" 
     android:textSize="10dp" 
     android:layout_alignParentBottom="true" 
     android:layout_toLeftOf="@+id/next_button" 
     /> 

</RelativeLayout> 
0

あなたのテーマは、あなたのAlertDialogボタンの色を反映していない場合は、役に立つことができること、また、プログラム的に管理することができます。

AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppCompatAlertDialogStyle)); 
    builder.setCancelable(false); 

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH); 
    String message ="message"; 
    builder.setTitle("title"); 

    builder.setMessage(message); 
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 
    // builder.setNegativeButton("No, Thanks", null); 
    //builder.show(); 
    AlertDialog alert = builder.create(); 
    alert.show(); 
    Button pbutton = alert.getButton(DialogInterface.BUTTON_POSITIVE); 
    pbutton.setTextColor(getResources().getColor(R.color.colorAccent)); 
    Button nbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE); 
    nbutton.setTextColor(getResources().getColor(R.color.accent)); 

あなたが必要な場合はここでは、両方のボタンの色を変更することができ、私は同じ問題を抱えていたし、これは私がそれを解決する方法である、それはあなたのため

0

をuserfulかもしれません願っています:

をのstyles.xmlで、あなたのテーマを宣言し、属性android:alertDialogTheme設定:...あなたがそうのようなAlertDialogを表示する場合は、今すぐ

<style name="YourTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- your theme attributes here --> 
    <item name="android:alertDialogTheme">@style/YourDialogTheme</item> 
</style> 

<style name="YourDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert" > 
    <!-- your dialog-theme attributes here --> 
</style> 

AlertDialog.Builder builder = new AlertDialog.Builder(activity); //where activity is an Activity with the theme attribute set to android:theme="@style/YourTheme" (in AndroidManifest) 
//... 
builder.show(); 

...ダイアログがaccentColorを持っている必要があり、すべてが以下のような新しいスタイルを作成しますが、@スタイル/ YourDialogThemeで指定したものに

2

を設定します。あなたのクラスで

<style name="AlertDialogCustom" parent="@android:style/Theme.Holo.Light.Dialog">   
    <!--buttons color--> 
    <item name="colorAccent">@color/actionable_items</item> 
    <!--item RadioButton or CheckBox color--> 
    <item name="colorControlActivated">@color/actionable_items</item> 
    <item name="colorPrimary">@color/actionable_items</item> 
    <item name="colorPrimaryDark">@color/actionable_items</item> 
    <item name="android:listChoiceIndicatorMultiple">@color/actionable_items</item> 
    <item name="android:listChoiceIndicatorSingle">@color/actionable_items</item> 
</style> 

そしてを:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.AlertDialogCustom));  
AlertDialog alertDialog = dialogBuilder.create();  
alertDialog.show(); 
3

AlertDialogのインポートを確認してください。それは古いAndroidバージョンで適用されるスタイルのためにv7サポートlibからインポートする必要があります。私は同じ問題を抱えていたと

import android.support.v7.app.AlertDialog 

import android.app.AlertDialog 

からインポート行を変更し、私を助けました。