Android Develop Tricks-1

Source: Internet
Author: User

Android Develop Tricks Set the size of the Alertdialog:

Alertdialog dialog = builder.settitle ("message list"). Setview (Layout). Create ();d ialog.show ();//Set the size of the window Dialog.getwindow (). SetLayout (300, 200);

Dialog.show (); Be sure to place the Dialog.getwindow (). setlayout (300, 200); the front, otherwise, does not work.


Another implementation:

Windowmanager.layoutparams params = Dialog.getwindow (). GetAttributes ();p arams.width = 300;params.height = 200; Dialog.getwindow (). SetAttributes (params);

In fact, SetLayout is the encapsulation of this method:

        Final Windowmanager.layoutparams attrs = GetAttributes ();        Attrs.width = width;        attrs.height = height;        if (mcallback! = null) {            mcallback.onwindowattributeschanged (attrs);        }


Show view at any location:

Add View via WindowManager:

Mwindowmanager = Getwindowmanager ();          Layoutinflater Inflater = (layoutinflater) getsystemservice (layout_inflater_service);          Mdialogview = inflater.inflate (R.layout.dialogview, null);          Windowmanager.layoutparams params = new Windowmanager.layoutparams ();          Params.height = WindowManager.LayoutParams.WRAP_CONTENT;          Params.width = WindowManager.LayoutParams.WRAP_CONTENT;          Params.y + + +;          Params.x + = -30;          Initdialogcomponents (Mdialogview);          Add dialog Box          mwindowmanager.addview (Mdialogview, params);

General method of touch Movement for any view: same as rewrite ontoucheent
Package Ui;import Android.content.context;import Android.util.attributeset;import android.util.DisplayMetrics; Import Android.util.log;import Android.view.motionevent;import Android.view.windowmanager;import Android.view.animation.alphaanimation;import Android.view.animation.animation;import Android.view.animation.animationset;import Android.view.animation.scaleanimation;import android.widget.Button;/*  * * * @author Chenxiruanhai * * */public class Menubutton extends Button {private Context mcontext;private WindowManager Mwm;int lastx;int lasty;int screenwidth;int screenheight;public Menubutton (context context, AttributeSet Attrs) {super ( context, attrs); this.mcontext = CONTEXT;MWM = (windowmanager) mcontext.getsystemservice (context.window_service);D Isplaymetrics DM = mcontext.getresources (). Getdisplaymetrics (); screenwidth = Dm.widthpixels;screenheight = Dm.heightpixels;} @Overridepublic boolean ontouchevent (Motionevent event) {super.ontouchevent (event); measure (0, 0); switch ( Event.getactIon ()) {Case MotionEvent.ACTION_DOWN:lastX = (int) event.getrawx (); lasty = (int) event.getrawy (); break;case MotionEvent.ACTION_MOVE:int dx = (int) event.getrawx ()-Lastx;int dy = (int) Event.getrawy ()-Lasty;int left = This.getl EFT () + dx;int top = this.gettop () + Dy;int right = This.getright () + Dx;int bottom = This.getbottom () + dy;if (left < 0) {left = 0;right = left + This.getwidth ();} if (right > ScreenWidth) {right = Screenwidth;left = Right-this.getwidth ();} if (Top < 0) {top = 0;bottom = top + this.getheight ();} if (Bottom > screenheight) {bottom = Screenheight;top = Bottom-this.getheight ();} This.layout (left, top, right, bottom), lastx = (int) event.getrawx (); lasty = (int) event.getrawy (); lastx = (int) event.getr AwX (); lasty = (int) event.getrawy (); break;case motionevent.action_up:{animation alphaanimation = new Alphaanimation ( 0.5F,1F); Alphaanimation.setduration (1000); Animation scaleAnimation2 = new Scaleanimation (1.0f,. 5f,1.0f,.5f); Scaleanimation2.setduration(500); Animationset animationset = new Animationset (true); Animationset.addanimation (alphaanimation); Animationset.addanimation (scaleanimation); Animationset.addanimation (ScaleAnimation2); Animationset.setfillafter (TRUE); Animation Currentanima = Getanimation (); if (Null!=currentanima) {currentanima.cancel (); Animationset.reset ();} Startanimation (Animationset);} break;} return true;}}

ImageView Set Property scaletype to Fit_start after removing extra whitespace

Imageview.setscaletype (Imageview.fit_start);        Imageview.setadjustviewbounds (TRUE);




Android Develop Tricks-1

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.