This article for everyone to share the Android Access view height of the method for your reference, the specific content as follows
getmeasuredheight ()AndgetheightThe difference
In fact, when the screen can wrap the content, their values are equal,
Only when the view is out of the screen can you see the difference:
Getmeasuredheight () is the size of the actual view, regardless of the screen,
The size of the getheight is now the size of the screen.
When the screen is exceeded, the getmeasuredheight () equals getheight () plus the size not displayed outside the screen
Specific methods
We know that View.getwidth and view.getheight in OnCreate cannot get the height and width of a view because the view component layout is completed after the Onresume callback.
here are three ways.
Getviewtreeobserver
Usegetviewtreeobserver (). Addongloballayoutlistener ()To get the width or height.
Ongloballayoutlistener is the inner class of the viewtreeobserver, which can be monitored by viewtreeobserver when the layout of a view tree changes, which is an observer (Observer) registering the Listening view tree, Be notified when global events in the View tree change. Viewtreeobserver cannot be directly instantiated, but is obtained by Getviewtreeobserver ().
In addition to Ongloballayoutlistener, Viewtreeobserver also has the following internal classes:
Interfaceviewtreeobserver.onglobalfocuschangelistener
The interface class of the callback function to be invoked when the focus state in a view tree changes
Interfaceviewtreeobserver.ongloballayoutlistener
The interface class of the callback function to invoke when the global layout changes in one view tree or when the visual state of a view in the view tree changes.
Interfaceviewtreeobserver.onpredrawlistener
The interface class of the callback function to invoke when a view tree will be drawn
Interfaceviewtreeobserver.onscrollchangedlistener
When some components in a view tree scroll, the interface class of the callback function that you want to call
Interfaceviewtreeobserver.ontouchmodechangelistener
The interface class of the callback function to invoke when a view tree's touch mode changes
Among them, we canusing Ongloballayoutlistener to get the true height of a view。
private int mheaderviewheight;
Private View Mheaderview;
.....
Mheaderview.getviewtreeobserver (). Addongloballayoutlistener (
new Ongloballayoutlistener () {
@Override Public
void Ongloballayout () {
mheaderviewheight = Mheaderview.getheight ();
Mheaderview.getviewtreeobserver ()
. Removeglobalonlayoutlistener (this);
}
);
Note, however, that Ongloballayoutlistener may be triggered multiple times, so after you get a height, you should log off the Ongloballayoutlistener.
The View post event gets
You can also get it in the Post method of view
public class Testheight extends activity {
TextView TV;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_activity_b);
TV = (TextView) Findviewbyid (R.id.textview);
Tv.post (New Runnable () {
@Override public
void Run () {
int height= tv.getheight ();}}
);
}
Direct measurement Calculation
int Intw=view.measurespec.makemeasurespec (0,view.measurespec.unspecified);
int Inth=view.measurespec.makemeasurespec (0,view.measurespec.unspecified);
Textview.measure (INTW, inth);
int intwidth = Textview.getmeasuredwidth ();
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.