Android custom component series [13]-Android custom dialog box is so simple, android is so simple
The dialog box is used in many places in our daily projects, but the dialog box provided by the Android system is not in coordination with our well-designed interface, in this case, we want to define the dialog box freely, or sometimes our dialog box is an image with no title or button, such as a series of requirements, this article introduces how to customize our dialog box like using Activity.
The following methods are available in the Custom dialog box:
1. Rewrite the Dialog.
2. Obtain the Window object Implementation of Dialog.
3. Use WindowManager.
4. Use DialogTheme.
Next we will introduce the second and fourth methods, and paste the encapsulated code. The third method will be discussed later in the case of WindowManager.
Reprint please explain the source: http://blog.csdn.net/dawanganban
1. Custom Dialog
Main Interface layout:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <Button android: id = "@ + id/dialog_custom_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Custom dialog box"/> <Button android: id = "@ + id/dialog_activity_button" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "Activity dialog box"/> </LinearLayout>
Two buttons are used to pop up two types of dialog boxes.
public class MainActivity extends Activity implements OnClickListener{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.dialog_custom_button).setOnClickListener(this);findViewById(R.id.dialog_activity_button).setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.dialog_custom_button:DialogCustom dc = new DialogCustom(MainActivity.this, R.layout.dialog);dc.setDismissButtonId(R.id.close_dialog);break;case R.id.dialog_activity_button:Intent intent = new Intent(MainActivity.this, DialogActivity.class);startActivity(intent);break;default:break;}}}The user-defined dialog box method is encapsulated for ease of use and can be encapsulated according to your project requirements.
Package com. example. testcustomdialog; import android. app. alertDialog; import android. app. alertDialog. builder; import android. content. context; import android. view. view; import android. view. view. onClickListener; import android. view. window;/*** public custom dialog box * @ author Administrator **/public class DialogCustom {private android. app. alertDialog. builder builder; private int layout; private AlertDialog dialog; privat E Window window; public DialogCustom (Context context, int layout) {builder = new AlertDialog. builder (context); this. layout = layout;} public DialogCustom (Context context, int theme, int layout) {builder = new AlertDialog. builder (context, theme); this. layout = layout;} public Builder getBuilder () {return builder;}/*** get the Window object of the dialog box * @ return */public Window getWindow () {dialog = builder. create (); dialog. sh Ow (); window = dialog. getWindow (); window. setContentView (layout); return window;}/*** get the corresponding View * @ param ID * @ return */public View getViewById (int id) through id) {if (window = null) getWindow (); return window. findViewById (id);}/*** set the ID of the button * @ param id */public void setDismissButtonId (int id) {View view = getViewById (id ); view. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View V) {dismiss () ;}}) ;}/ *** close dialog box */public void dismiss () {if (dialog! = Null) {dialog. dismiss ();}}}You can create a Dialog object and add a listener View. The main work is in the layout file, that is, the content of your Dialog box.
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <LinearLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: orientation = "vertical" android: layout_gravity = "center_horizontal" android: gravity = "center_horizontal" android: paddingBottom = "30dip" android: background = "# ffffff"> <TextView android: layout_width = "260dip" android: layout_height = "40dip" android: layout_margin = "30dip" android: gravity = "center" android: text = "this is ActivityDialog"/> <Button android: id = "@ + id/close_dialog" android: layout_width = "100dip" android: layout_height = "30dip" android: text = "click to close" android: background = "@ drawable/close_butten_p"/> </LinearLayout>
Ii. Use DialogTheme
<style name="DialogActivityTheme" parent="android:style/Theme.Dialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:width">1px</item> <item name="android:height">1px</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:backgroundDimEnabled">true</item> </style>
Register DialogActivity in ActivityManifest. xml
<activity android:name="com.example.testcustomdialog.DialogActivity" android:theme="@style/DialogActivityTheme" android:screenOrientation="portrait"></activity>
In this way, we can create a dialog box just like using a normal Activity. Isn't it easy.
3. Two types of dialog box Summary
A dialog box that appears to be the same during running is very different. The dialog box created in the second method is actually an Activity, so it has the lifecycle and all features of the Activity. It will affect the lifecycle of other activities in the stack. In addition, if there is an input box to be entered in the dialog box, it is recommended that you use this method to automatically call up the input method and adapt the screen automatically. The first method is especially convenient to create a dialog box, we recommend that you use this function when you need to display a prompt message. (Source Code download: http://download.csdn.net/detail/lxq_xsyu/8315023)
CSDN blog star activity began, vote for me: http://vote.blog.csdn.net/blogstar2014/details? Username = lxq_xsyu # content