How to change theme for AlertDialog

前端 未结 15 2125
名媛妹妹
名媛妹妹 2020-11-22 09:14

I was wondering if someone could help me out. I am trying to create a custom AlertDialog. In order to do this, I added the following line of code in styles.xml



        
15条回答
  •  旧巷少年郎
    2020-11-22 09:52

    I was struggling with this - you can style the background of the dialog using android:alertDialogStyle="@style/AlertDialog" in your theme, but it ignores any text settings you have. As @rflexor said above it cannot be done with the SDK prior to Honeycomb (well you could use Reflection).

    My solution, in a nutshell, was to style the background of the dialog using the above, then set a custom title and content view (using layouts that are the same as those in the SDK).

    My wrapper:

    import com.mypackage.R;
    
    import android.app.AlertDialog;
    import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class CustomAlertDialogBuilder extends AlertDialog.Builder {
    
        private final Context mContext;
        private TextView mTitle;
        private ImageView mIcon;
        private TextView mMessage;
    
        public CustomAlertDialogBuilder(Context context) {
            super(context);
            mContext = context; 
    
            View customTitle = View.inflate(mContext, R.layout.alert_dialog_title, null);
            mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
            mIcon = (ImageView) customTitle.findViewById(R.id.icon);
            setCustomTitle(customTitle);
    
            View customMessage = View.inflate(mContext, R.layout.alert_dialog_message, null);
            mMessage = (TextView) customMessage.findViewById(R.id.message);
            setView(customMessage);
        }
    
        @Override
        public CustomAlertDialogBuilder setTitle(int textResId) {
            mTitle.setText(textResId);
            return this;
        }
        @Override
        public CustomAlertDialogBuilder setTitle(CharSequence text) {
            mTitle.setText(text);
            return this;
        }
    
        @Override
        public CustomAlertDialogBuilder setMessage(int textResId) {
            mMessage.setText(textResId);
            return this;
        }
    
        @Override
        public CustomAlertDialogBuilder setMessage(CharSequence text) {
            mMessage.setText(text);
            return this;
        }
    
        @Override
        public CustomAlertDialogBuilder setIcon(int drawableResId) {
            mIcon.setImageResource(drawableResId);
            return this;
        }
    
        @Override
        public CustomAlertDialogBuilder setIcon(Drawable icon) {
            mIcon.setImageDrawable(icon);
            return this;
        }
    
    }
    

    alert_dialog_title.xml (taken from the SDK)

    
    
        
    
                
                
            
            
    
    

    alert_dialog_message.xml

    
    
        
    
    

    Then just use CustomAlertDialogBuilder instead of AlertDialog.Builder to create your dialogs, and just call setTitle and setMessage as usual.

提交回复
热议问题