Control the display size and position of the activity in the dialog box style.

Source: Internet
Author: User

Control the display size and position of the activity in the dialog box style.

Project development needs, because the current project is nearing completion, the user proposes to modify the conditional filtering method, in order to achieve the minimum change to meet the user's needs, a variety of Baidu, popupWindow is widely used for the style of the dialog box. However, since the activity dialog box is used for development and design, we will introduce how to implement the same effect as PopupWindow through activity, not much nonsense.

To implement a dialog box style activity, we need to add a style declaration in AndroidManifest. xml:

<activity  android:name=".product.MyselfPayProduct"   android:screenOrientation="portrait"   android:theme="@android:style/Theme.Dialog" >

However, such a dialog box style often cannot meet our needs, and the display effect is not so satisfactory. The first point is how to control the dialog box size:

// Window alignment screen width Window win = this. getWindow (); win. getDecorView (). setPadding (0, 0, 0, 0); WindowManager. layoutParams lp = win. getAttributes (); lp. width = WindowManager. layoutParams. MATCH_PARENT; lp. height = WindowManager. layoutParams. WRAP_CONTENT; lp. gravity = Gravity. TOP; // set the dialog box to display win. setAttributes (lp );

Add this control statement to the onClick () method of the activity in our dialog box, so that the width of the dialog box can be the same as that of the screen, lp. gravity = Gravity. TOP; // The Setting dialog box is displayed on the TOP. The android default dialog box is displayed in the center. You can use this code to set the display position of the dialog box.

Has it reached your satisfaction? The following describes how to use activity to display the result by clicking the plus sign in the upper right corner. To display this effect, we need to adjust the position of the dialog box through android: layout_marginTop = "50dp" in the layout file. The default pop-up box for Android is very ugly, to achieve a better display effect, we add an animation here:

Enter the animation:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <scale        android:fromXScale="1.0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:toXScale="1.0"         android:fromYScale="0.0"        android:toYScale="1.0"        android:duration="200"        android:pivotX="0"        android:pivotY="10%"        /></set>

Exit Animation:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <scale        android:fromXScale="1.0"        android:interpolator="@android:anim/accelerate_decelerate_interpolator"        android:toXScale="1.0"         android:fromYScale="1.0"        android:toYScale="0.0"        android:duration="200"        android:pivotX="0"        android:pivotY="10%"        /></set>

Android animation files are generally placed in the anim folder of res. By default, this folder does not exist and needs to be manually added.

Next, we need to add the android style File: style. xml.

<Resources> <! -- Base application theme, dependent on API level. this theme is replaced by AppBaseTheme from res/values-vXX/styles. xml on newer devices. --> <style name = "AppBaseTheme" parent = "android: Theme. light "> <! -- Theme customizations available in newer API levels can go in res/values-vXX/styles. xml, while customizations related to backward-compatibility can go here. --> </style> <! -- Application theme. --> <style name = "AppTheme" parent = "AppBaseTheme"> <! -- All mizmizations that are NOT specific to a special API-level can go here. --> </style> <! -- No title --> <style name = "notitle" parent = "AppBaseTheme"> <item name = "android: windowNoTitle"> true </item> </style> <! -- Similar to the dialog box --> <style name = "MyDialogTopRight"> <item name = "android: windowBackground"> @ android: color/transparent </item> <item name = "android: Movie wistranslucent"> true </item> <item name = "android: windowNoTitle "> true </item> <item name =" android: windowAnimationStyle "> @ style/Anim_scale </item> </style> <style name =" Anim_scale "parent =" @ android: style/Animation. activity "> <item name =" android: activityOpenEnterAnimation "> @ anim/scale_in </item> <item name =" android: activityOpenExitAnimation "> @ anim/scale_out </item> <item name =" android: activityCloseEnterAnimation "> @ anim/scale_in </item> <item name =" android: activityCloseExitAnimation "> @ anim/scale_out </item> </style> </resources>

Finally, we need to modify the statement in the AndroidManifest. xml file:

android:theme="@style/MyDialogTopRight"

At this point, the style of the activity dialog box is displayed perfectly.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.