First, get the screen height 1, the Android interface simple introduction
To get a high screen width, we can start with the Android interface to understand
Android interface mainly consists of three parts:1, status bar 2, title bar 3, content area
\
(1) Status bar
The status bar is used primarily to display some system icons, app notification icons, and system time.
(2) title bar
The title bar of Android is mainly used to display the current position, after 3.0 added Actionbar, with navigation and Optionmenu features, 5.0 new Toolbar control added, and Actionbar similar, but more custom space
(3) Content area
The content area in Android is actually a single frame layout with the default name Android.id.content, and the Setcontentview () method is equivalent to adding a view to this single frame layout and overwriting it.
2. Get the screen width high
protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Displaymetrics dm = new Displaymetrics (); Get screen information Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM); int screenwidth = dm.widthpixels; int screenheigh = dm.heightpixels; LOG.V ("Get screen width", "width:" + screenwidth + ", Height:" + screenheigh); }
Get to the screen width high print out
Second, get control width height
Control view has the GetHeight () and GetWidth () methods to get a wide height, but if you get the control's height directly in the OnCreate () method, the value obtained is 0, as for the reason because the OnCreate () method only provides data initialization There is no formal drawing at this time. The drawing is done in OnDraw, and the calculation is too late. The easy way to think about it is to expect to get its width and height immediately after the program has just measured a specified control, or initialize the data. This requires a way to listen to this event, fortunately, Android provides such a mechanism, using the Getviewtreeobserver method in the view class, you can get to the observer of the specified view, the moment before the control to draw a callback, so that the speed does not delay , the data obtained is accurate.
Referenced from: http://it.warmtel.com/?p=813
Method One:
public class Mainactivity extends Appcompatactivity { TextView firsttxt; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Firsttxt = (TextView) Findviewbyid (r.id.hello_word_txt); Viewtreeobserver viewtreeobserver = Firsttxt.getviewtreeobserver (); Viewtreeobserver.addonpredrawlistener (New Viewtreeobserver.onpredrawlistener () { @Override public Boolean Onpredraw () { int height = firsttxt.getheight (); int width = firsttxt.getwidth (); LOG.V ("Get textview width High", "width:" + width + ", Height:" + height); return true;}} );} }
Get to the wide-height print display:
Method Two:
As for method Two, here's a simple understanding of my own
Add a runnable to the layout queue: View.post ()
Simply put, just use View.post () a runnable. The methods in the Runnable object are triggered after the view's measure, layout, and other events
The UI event queue processes events sequentially. After Setcontentview () is called, the event queue contains a message that requires re-layout, so anything you post to the queue will be executed after the layout has changed.
Firsttxt.post (New Runnable () { @Override public void Run () { firsttxt.getheight ();//height are ready Firsttxt.getwidth (); } });
Some understandings about the method two
As mentioned above , the drawing control is done in the OnDraw () method, it is too late to get the control's height in OnDraw, and it is too early to measure in Onmeasure (). let's try to get the control's width high in onlayout (). Both OnLayout () and OnDraw () are callback methods in the control view life cycle. As for the life cycle of the view, I have not touched, and so on the back in-depth study and then summed up.
First create a new class to inherit from TextView, and then rewrite the OnLayout () and Onmeasure () methods inside
public class Mytextview extends TextView {public Mytextview (context context) {super (context); } @TargetApi (Build.version_codes. LOLLIPOP) Public Mytextview (context context, AttributeSet attrs, int defstyleattr, int defstyleres) {SUPER (cont Ext, Attrs, defstyleattr, defstyleres); } public Mytextview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr) ; } public Mytextview (context context, AttributeSet Attrs) {Super (context, attrs); }//=========== above is inherited TextView default implementation method, we can regardless of @Override protected void onmeasure (int widthmeasurespec, int Heightmea Surespec) {super.onmeasure (Widthmeasurespec, heightmeasurespec);//Get Mytextview The current instance of high int height = This.geth Eight ();//Gets Mytextview The current instance's width int width = this.getwidth ();//LOG.V print output shows LOG.V ("Onmeasure Get control width High", "High:" + Hei Ght + ", Width:" + width); } @Override protected void OnLayout (Boolean changed, int left, int top, int right, int bottom) {super.onlayout (changed, left, top, right, bottom);//Get Mytextview The current instance of high int height = t His.getheight ();//Gets Mytextview The current instance's width int width = this.getwidth ();//LOG.V print output shows LOG.V ("OnLayout Get control wide height", "High: "+ Height +", Width: "+ width"; }}
The custom Mytextview is instantiated in the OnCreate () method in the acitvity of the program entry.
public class Mainactivity extends Appcompatactivity { Mytextview mmytextview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mmytextview = (Mytextview) Findviewbyid (r.id.mytextview_txt); }}
Run Print display:
From the above you can see that Mytextview first callback Onmeasure () method, has not yet obtained the control's wide-height data. As mentioned above, "If you get the control's height directly in the OnCreate () method, and the value obtained is 0", then we can assume that the activity's OnCreate () method executes before the view executes the first onmeasure () method, We try to see if this hypothesis can be verified by the following methods
First add two of our custom methods in the new Mytextview above, one is setmytextviewsize (), the other is Getmytextviewsize (), and the code is as follows:
public class Mytextview extends TextView {int mmytextviewheight; int mmytextviewwidth; Public Mytextview (Context context) {super (context); } @TargetApi (Build.version_codes. LOLLIPOP) Public Mytextview (context context, AttributeSet attrs, int defstyleattr, int defstyleres) {SUPER (cont Ext, Attrs, defstyleattr, defstyleres); } public Mytextview (context context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr) ; } public Mytextview (context context, AttributeSet Attrs) {Super (context, attrs); }//=========== above is inherited TextView default implementation method, we can regardless of @Override protected void onmeasure (int widthmeasurespec, int Heightmea Surespec) {super.onmeasure (Widthmeasurespec, Heightmeasurespec); Gets the high int height = this.getheight () of the current instance of Mytextview; Gets the width int width = this.getwidth () of the current instance of Mytextview; Call Setmytextviewsize so that other classes can be obtained through getmytextviewsize () to//mmytextviewheight and MmyThe value of the Textviewwidth variable, which is high and wide setmytextviewsize (height, width); The LOG.V printout shows LOG.V ("Onmeasure gets control wide height", "High:" + height + ", Width:" + width); } @Override protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {Super.onlayo UT (changed, left, top, right, bottom); Gets the high int height = this.getheight () of the current instance of Mytextview; Gets the width int width = this.getwidth () of the current instance of Mytextview; With the Setmytextviewsize (height, width); The LOG.V printout shows LOG.V ("OnLayout gets control wide height", "High:" + height + ", Width:" + width); } public void setmytextviewsize (int height, int width) {mmytextviewheight = height; Mmytextviewwidth = width; } public int[] Getmytextviewsize () {int[] viewsize = new int[]{mmytextviewheight, mmytextviewwidth}; return viewsize; }}
The Mytextview getmytextviewsize () method that has been instantiated above in the Acitvity OnCreate () method of the program entry attempts to get the control's height
public class Mainactivity extends Appcompatactivity { Mytextview mmytextview; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Mmytextview = (Mytextview) Findviewbyid (r.id.mytextview_txt); int[] mytextviewsize = Mmytextview.getmytextviewsize (); LOG.V ("entry acitivity OnCreate get control width High", "High:" + mytextviewsize[0] + ", Width:" + mytextviewsize[1]);} }
Run Print output display:
The OnCreate () method of acitivity can be found by the time it was executed before the first callback Onmeasure () of the view, when the view first callback Onmeasure () method was not acquired to the width of the high, The OnCreate () method of the natural acitivity also fails to get the control's height. So, with the printed information above, we can assume that if the OnLayout () method has obtained the view's wide height, the activity's OnCreate () will get a wider line. One of the ideas I came up with was to iterate over the OnLayout () method of Mytextview in the Acitivity OnCreate () method , but we didn't do it until the main thread of Android, the UI thread, would cause the page to display. Then we should open up a sliver thread in the OnCreate () method, and the method loops through the Mytextview's OnLayout () method until it gets to the wide height. To realize this idea, we can use the handler.
The following is a specific code implementation:
public class Mainactivity extends appcompatactivity {Mytextview mmytextview; When using the Handler default parameterless construction method, the thread that Handler executes declares the thread of Handler, which is here in the main thread, which is the UI thread Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {LOG.V ("handler gets control wide height", "High:" + Msg.arg1 + ", Width:" + msg.arg2); } }; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mmytextview = (Mytextview) Findviewbyid (r.id.mytextview_txt); New Thread (New Runnable () {@Override public void run () {int[] viewsize = Mmytextvie W.getmytextviewsize (); LOG.V ("onCreate () Get control width High", "High:" + viewsize[0] + ", Width:" + viewsize[1]); while (viewsize[0] = = 0) {viewsize = Mmytextview.getmytextviewsize (); if (viewsize[0]! = 0) {Message msg = MESSAGE.OBTAIn (); MSG.ARG1 = viewsize[0]; MSG.ARG2 = viewsize[1]; Mhandler.sendmessage (msg); }}}). Start (); }}
Run Print output display:
By the above method we can get the width of view in the OnCreate () method.
Android get control wide height and screen width height