Change color of window frame

后端 未结 2 1171
渐次进展
渐次进展 2020-12-19 14:49

How do I go about changing the color of the frame of this dialog? I\'ve tried a bunch of things and nothing works.



        
相关标签:
2条回答
  • 2020-12-19 15:39

    You mean white frame ? I think it's a part of 9-patch drawable you can look up how Theme.Dialog is build in the SDK_FOLDER\platforms\android-sdkversion\data\res\values and then styles.xml and themes.xml

    As i've said, the white frame is a part of the background image. its panel_background.9.png So if you want to change frame - you'll need different background image + need to overwrite in style setting it.

     <item name="android:windowBackground">@android:drawable/panel_background</item> 
    

    and you'll need to define a style that will be derived from Theme.Dialog and have this

    <item name="android:windowBackground">@drawable/your_drawable</item> 
    

    so if you put in styles.xml something like

    <style name="NewBorderDialogTheme" parent="android:Theme.Dialog">
     <item name="android:windowBackground">@drawable/your_drawable</item> 
    </style>
    

    Put new drawable and set your activity to the new theme - you should see your new border

    0 讨论(0)
  • 2020-12-19 15:45

    If you want to do it Programatically, try below code:

    What you have to do:

    When AlertDialog is visible on your screen, OnShowListener is called. So, by adding dialog.setOnShowListener(this) you will be able to customize your AlertDialog.

    Code:

    // Create AlertDialog
    AlertDialog.Builder adb = new AlertDialog.Builder(context1);
        adb.setTitle(context1.getString(R.string.app_name))
        .setMessage(message)
        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
    
            }
    });
    AlertDialog dialog = adb.create();
    
    // Make some UI changes for AlertDialog
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(final DialogInterface dialog) {
    
            // Add or create your own background drawable for AlertDialog window
            Window view = ((AlertDialog)dialog).getWindow();
            view.setBackgroundDrawableResource(R.drawable.your_drawable);
    
            // Customize POSITIVE, NEGATIVE and NEUTRAL buttons.
            Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            positiveButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
            positiveButton.setTypeface(Typeface.DEFAULT_BOLD);
            positiveButton.invalidate();
    
            Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
            negativeButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
            negativeButton.setTypeface(Typeface.DEFAULT_BOLD);
            negativeButton.invalidate();
    
            Button neutralButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEUTRAL);
            neutralButton.setTextColor(context1.getResources().getColor(R.color.primaryColor));
            neutralButton.setTypeface(Typeface.DEFAULT_BOLD);
            neutralButton.invalidate();
        }
    });
    
    0 讨论(0)
提交回复
热议问题