Activity Control dialog box style, display size and position _android

Source: Internet
Author: User
Tags windows snap xmlns

Project development needs, as the project is nearing completion, the user proposed to modify the conditions, to achieve the smallest changes to realize the user's needs, all kinds of Baidu, for the dialog box style, we generally use Popupwindow, but because of the previous development of the use of the design of the Activity dialog box way, So today for you to introduce, how to achieve the same effect with Popupwindow through activity, nonsense not much talk now start dry.

To implement the dialog-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 dialog style often does not meet our needs, the display of the effect is not so satisfactory, the 1th is how to control the size of the dialog box :

Windows Snap to 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;//Settings dialog box with the top display
win.setattributes (LP);

Add this control statement to our dialog activity's onclick () method, so that our dialog can be as wide as the screen, lp.gravity = gravity.top;//Settings dialog box display, Android Default dialog box centered , we can set the display position of the dialog box through this code.

Has it reached your satisfaction to be here? Here is a brief introduction to you, how to achieve micro-letter in the upper right corner of the display by clicking the plus sign. To do this, we need to adjust the position of the dialog box through the android:layout_margintop= "50DP" in the layout file, the Android default pop-up box is very ugly, in order to achieve better display effect, Here we add a display animation effect:

Enter 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= "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= "android:pivotx=" "
    0"
    android:pivoty= "10%"
    />
</set>

The Android animation file is typically placed in the Res Anim folder, and the default folder does not exist, and we need to add it manually.

Below we need to add our animations to the Android style file: Style.xml

<resources> <!--Base application theme, dependent on API level.
  This theme was replaced by Appbasetheme from Res/values-vxx/styles.xml on newer devices. --> <style name= "Appbasetheme" parent= "Android:Theme.Light" > <!--Theme customizations available I  N Newer API levels can go on res/values-vxx/styles.xml while customizations related to Backward-compatibility
    Can go. --> </style> <!--application theme. --> <style name= "Apptheme" parent= "Appbasetheme" > <!--all customizations this are not specific to a par Ticular Api-level can go. --> </style> <!--no title--> <style name= "Notitle" parent= "Appbasetheme" > <item name= "and  
    Roid:windownotitle ">true</item> </style> <!--Similar dialog box effect--> <style name=" Mydialogtopright "> <item name= "Android:windowbackground" > @android:color/transparent</item> <item name= "andRoid:windowistranslucent ">true</item> <item name=" Android:windownotitle ">true</item> <it EM 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 N Ame= "Android:activitycloseenteranimation" > @anim/scale_in</item> <item name= "Android: Activitycloseexitanimation "> @anim/scale_out</item> </style> </resources>

Finally, we need to revise our statement in the Androidmanifest.xml file:

android:theme="@style/MyDialogTopRight"

Here we have the perfect implementation of the activity of the dialog box style display.

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring some help, but also hope that a lot of support cloud Habitat community!

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.