Explain the use of dialog in Android

In Android, dialog is often used to realize some prompts and special effects, and the style is also different. You have to check a lot of data every time, which may not be solved. Here is a summary of some common dialog practices.

Ordinary dialog

Here, use alertdialog to display a system prompt dialog box. The effect is as follows:

Modify the position, size and transparency of the normal dialog box

It is mainly to add the following code under the ordinary dialog. Show()

Here, set the dialog height to 4 / 10 of the screen height, the width to 6 / 10 of the screen width, the colleague position to the bottom, and the transparency to translucent. Of course, there are many other properties, which will not be introduced here. You can try them yourself. The effects are as follows:

Use the normal dialog to add a custom layout

We need to customize a layout as follows:

We have created a new layout with a height and width of 100dp. The linear layout is wrapped with a textview. The layout is very simple. Of course, you can customize a complex layout, which will not be introduced here. Let's look at the implementation of Java code.

We directly set our layout through the builder to see the effect:

The dialog here is very ugly, which is related to the default theme of alertdialog. Let's change the style of the dialog by customizing the theme to make the dialog beautiful. In values / styles.xml, the custom style inherits Android: theme.dialog to implement its own style

Here, the attributes of styles are annotated. No style is necessary. You can try to change some values to see the effect in order to achieve your best effect. When creating a dialog, transfer the style

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.MyCommonDialog);

Now the effect is as follows:

It can be seen that the height and broadband of our layout still have no effect. We know that the layout of subspace is generally measured by the layout, so I thought of setting a layout for the outermost layer of this layout to see whether we can achieve our effect.

Modify dialog_ The layout of custom1.xml is as follows:

Run again as follows:

We have achieved the desired effect, so that you can introduce styles and custom layouts to achieve the effects of various dialog boxes.

Inherit dialog to implement dialog

By inheriting dialog to implement a custom dialog, we can directly new our dialog anywhere and implement a specific dialog.

1. Create a new style mydialog in values / styles.xml

2. Create a new mydialog and inherit dialog

/** LP. X and LP. Y indicate the offset relative to the original position. * when the parameter value contains gravity.left, the dialog box appears on the left, so LP. X indicates the offset relative to the left, and the negative value is ignored. * when the parameter value contains gravity.right, the dialog box appears on the right, so LP. X indicates the offset relative to the right, and the negative value is ignored. * when the parameter value contains gravity.top, the dialog box appears on the top, Therefore, LP. Y represents the offset from the upper edge, and the negative value is ignored. * when the parameter value contains gravity.bottom, the dialog box appears at the lower edge, so LP. Y represents the offset from the lower edge, and the negative value is ignored. * when the parameter value contains gravity.center_ When horizontal, the dialog box is centered horizontally, so LP. X means to move * LP. X pixels in the horizontal center. Positive values move to the right and negative values move to the left. * when the parameter value contains gravity.center_ In vertical, the dialog box is vertically centered, so LP. Y means to move the LP. Y pixel in the vertical center, and the negative value moves to the left. * the default value of gravity is gravity.center, that is, gravity.center_ HORIZONTAL | Gravity.CENTER_ VERTICAL */

Here we explain some parameters of window. I set the dialog box 100px from the top left corner to the top.

3. Use mydialog

Custom layout dialog_ custom2.xml

Java code

4. View the effect:

Animate dialog

1. Create a new animation file

Enter the animation dialog_ enter.xml

Exit animation dialog_ exit.xml

2. Create a new style in values / styles.xml

It is mainly to specify the new animation for Android: windowanimationstyle. The reference is the same as the previous one, which is given here,

3. View the effect

Inherit dialog to realize the pop-up dialog at the bottom

Customize mybottomdialog

The height and width of the dialog specified in the oncreate method

Layout dialog_ custom3.xml

The method of use is the same

There is no need to set the layout here, because we have preloaded the layout and set the click event in the construction method of mybottomdialog

View effect:

Customize the pop-up dialog imitating Meun

Code for mymenudialog

I won't introduce it. This is mainly realized by using the X, y and gravity of windowmanager.layoutparams. Of course, you can customize the pop-up animation of dialog to realize a menu dialog box.

The effects are as follows:

Basically, dialog has achieved these effects, which should meet the needs of most projects. As for the following complex dialog with listview and GridView, you can inherit dialog through custom dialog. You can just follow the gourd and draw a gourd. You can supplement what you encounter in the future.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>