Android Dialog related settings

1.AlertDialog width and height settings, set the maximum height

AlertDialog can setView to customize the view, or directly use setMultiChoiceItems and setSingleChoiceItems, so it is more flexible and frequently used.

However, the height of AlertDialog will change with the content it contains. If it contains a lot of content, its height may fill the entire screen, which is visually ugly. Therefore, you can set the maximum height to make it more beautiful and natural.

1) If you know the size of the specific content:

window window = dialog.getWindow();

int width = getResources().getDisplayMetrics().w idthPixels;

int height = getResources().getDisplayMetrics().h eightPixels;

window.setLayout(width-100,height*3/4);

Note: It must be set after dialog.show(), otherwise it will have no effect. For the specific size, set the width and height in the method setLayout(), and the size can be adjusted by yourself.

2) Uncertain amount of content

①Use setMultiChoiceItems: View its source code, you can get a listview, and then set the monitor for layout changes.

final ListView listView = ((AlertDialog) dialog).getListView();

//Set the width and height of the dialog, sometimes it is not very beautiful

listView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

    @Override

    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        int height = v.getHeight();

        int maxHeight = getResources().getDispla yMetrics().heightPixels*5/7;

        if(height > maxHeight){

            ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();

            layoutParams.height = maxHeight;

            listView.setLayoutParams(layoutParams);

        }

    }

});

②Use setView to customize the dialog: at this time, the view is the parameter passed in by the setView method

 //Set the width and height of the dialog, sometimes it is not very beautiful

view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {

    @Override

    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {

        int height = v.getHeight();

        int maxHeight = getResources().getDisplayM etrics().heightPixels*5/7;

        if(height>maxHeight){

            view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, maxHeight));

        }

    }

});

 

2. Set the color of Dialog's titlie and other colors

The principle of using reflection: the source code can see a variable AlertController, use reflection to get this variable, and then you can set it accordingly.

00cba9a743bc44268daa08744bbfbcda.png

 Field mAlert = AlertDialog.class.getDeclaredFie ld("mAlert");

mAlert.setAccessible(true);

Object Controller = mAlert.get(dialog);

//Modify message information

Field mMessage = Controller.getClass().getDec laredField("mMessageView");

mMessage.setAccessible(true);

TextView mMessageView = (TextView) mMessage.get(Controller);

mMessageView.setPadding(100,5,5,5);

//title style modification color

Field mTitle = Controller.getClass().getDeclar edField("mTitleView");

mTitle.setAccessible(true);

TextView mTitleView = (TextView) mTitle.get(Controller);

mTitleView.setTextColor(Color.RED);

// get button

Button negative = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);

negative.setTextColor(Color.BLUE);

negative.setTextSize(16);

// get button

Button positive = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);

positive.setTextSize(16);

f8fcd62062e6482f8cec36d901bfcc3c.png

 

Guess you like

Origin blog.csdn.net/zenmela2011/article/details/127654485