Foreword:
Dialogs are used in many places in the project. It is too ugly to use system dialogs, so I wrote a custom dialog myself.
The dialog box includes: 1. Rounded corners
2. App icon, prompt text, "OK" button to close the dialog
Difficulties: 1. Rounded corner display of dialog box border
2. Considering that the number of characters in the prompt text is uncertain, the text information of the prompt needs to be displayed in one line without affecting the appearance.
3. Set the width and height of the dialog box
Technical reserve:
1. Android development_Use AlertDialog to implement dialogs Know that AlertDialog has setView(view) and Dialog has ContentView(view) method.
2. Android project combat (5): TextView displays text information in one line with adaptive size . When the number of text characters is small, the text size is large, and when the number of text characters is large, the text size is small.
--------------------------------------------------------------------------------------------------------------------
1. Layout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" xmlns:autofit="http://schemas.android.com/apk/res-auto" android:layout_height="wrap_content" android:background="@drawable/dialog_corner_bg" android:paddingBottom="@dimen/dp_16" > <ImageView android:id="@+id/dialog_img" android:layout_width="30dp" android:layout_height="30dp" android:src="@mipmap/icon1" android:layout_marginTop="@dimen/dp_12" android:layout_centerHorizontal="true" /> <me.grantland.widget.AutofitTextView android:id="@+id/dialog_txt_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:maxLines="1" android:textSize="14sp" autofit:minTextSize="10sp" android:text="下载失败,请重试" android:gravity="center" android:layout_margin="@dimen/dp_6" android:layout_centerInParent="true" /> <TextView android:id="@+id/dialog_btn_comfirm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:gravity="center" android:background="@drawable/bg_btn_blue_big" android:textColor="@color/white" android:paddingTop="@dimen/dp_6" android:paddingBottom="@dimen/dp_6" android:paddingLeft="@dimen/dp_30" android:paddingRight="@dimen/dp_30" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" /> </RelativeLayout>
where the root container is used
android:background="@drawable/dialog_corner_bg"
This is the shape to set the edge rounding
<?xml version="1.0" encoding="utf-8"?> <!-- rounded corners for setting the info dialog --> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="@dimen/dp_12"></corners> <solid android:color="@color/white"></solid> </shape>
2. From the above, you can see that only one drawable file is needed to set the rounded corners of the dialog box, and the shape can be set to the corners property .
Maybe you have found from other articles that some people use rounded background images to achieve rounded corners of dialog boxes, and some people use styles to achieve rounded corners of dialog boxes.
After more than an hour of tossing, I found that these methods are unreliable. In fact, it is very simple. The above methods use AlertDialog, but the Dialog class we use here, a shape is enough.
Because there must be more dialog boxes in the project, so I write a static method to pass the context parameters and the content of the prompt text:
public static void showEditDialog(Context context , String message) {}
1. Initialization dialog related operations:
View view = LayoutInflater.from(context).inflate(R.layout.dialog_message, null); TextView confirm; // OK button final TextView content; // Content confirm = (TextView) view.findViewById(R.id.dialog_btn_comfirm); content = (TextView) view.findViewById(R.id.dialog_txt_content); content.setText(message); final Dialog dialog = new Dialog(context); dialog.setContentView(view); dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); //Set the dialog background to be transparent, it will not work for AlertDialog
2. Set the click event of the "OK" button
confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } });
3. Display the dialog
dialog.show();
4. Set the width and height of the dialog box
DisplayMetrics dm = context.getResources().getDisplayMetrics(); int displayWidth = dm.widthPixels; int displayHeight = dm.heightPixels; android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); // Get the current parameter value of the dialog p.width = ( int ) (displayWidth * 0.55 ); // The width is set to 0.55 p of the screen .height = ( int ) (displayHeight * 0.28 ); //The height is set to 0.28 of the screen dialog.setCanceledOnTouchOutside( false ); // Set the click on the screen Dialog does not disappear dialog.getWindow().setAttributes(p); // Set take effect
Note: This is to use the aspect ratio of the screen to set the aspect ratio of the dialog box.
There is another way:
AlertDialog dialog = builder.create(); dialog.setView(view); dialog.show(); WindowManager m = getWindowManager(); //Here you will find that you cannot call the getWindowManager() method if you are not in the activity, so this method cannot be used Display d = m.getDefaultDisplay(); // To get the screen width and height android.view .WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); // Get the current parameter value of the dialog p.height = ( int ) (d.getHeight() * 0.3 ); // The height is set to 0.3 of the screen p.width = ( int ) (d.getWidth() * 0.5 ); // The width is set to 0.5 of the screen dialog.getWindow().setAttributes(p); // The setting takes effect
Dialog interface when there is a lot of prompt text:
Full code:
/*----------------------------dialog---------------------------------*/ public static void showEditDialog(Context context , String message) { View view = LayoutInflater.from(context).inflate(R.layout.dialog_message, null); TextView confirm; // OK button final TextView content; // Content confirm = (TextView) view.findViewById(R.id.dialog_btn_comfirm); content = (TextView) view.findViewById(R.id.dialog_txt_content); content.setText(message); final Dialog dialog = new Dialog(context); dialog.setContentView(view); dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); confirm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); DisplayMetrics dm = context.getResources().getDisplayMetrics(); int displayWidth = dm.widthPixels; int displayHeight = dm.heightPixels; android.view.WindowManager.LayoutParams p = dialog.getWindow().getAttributes(); // Get the current parameter value of the dialog p.width = ( int ) (displayWidth * 0.55 ); // The width is set to 0.5 p of the screen .height = ( int ) (displayHeight * 0.28 ); // The width is set to 0.5 of the screen dialog.setCanceledOnTouchOutside( false ); // Set the click on the screen Dialog does not disappear dialog.getWindow().setAttributes(p); // The setting takes effect }
--------------------------------------------------------------------------------------------------------
I currently single out two apps, responsible for leancloud cloud development, students can pay attention to me as a little programmer, and help each other to solve problems.