Android Get View height

Source: Internet
Author: User

the difference between getmeasuredheight () and GetHeightin 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, getmeasuredheight () equals getheight () plus the size not shown outside the screen

Specific Methods

We know that view.getwidth and view.getheight cannot get the height and width of a view in OnCreate because the View component layout is done after the Onresume callback.

Here are three ways of saying

Getviewtreeobserver

Use Getviewtreeobserver (). Addongloballayoutlistener () to get the width or height.

ongloballayoutlistener is the inner class of viewtreeobserver and can be heard by Viewtreeobserver when the layout of a view tree changes , which is an observer (Observer) that registers the listener view tree and is notified when the global event of the view tree changes. Viewtreeobserver cannot be instantiated directly, but is obtained through Getviewtreeobserver ().
In addition to Ongloballayoutlistener, Viewtreeobserver has the following inner classes:
Interfaceviewtreeobserver.onglobalfocuschangelistener
the interface class for the callback function to invoke when the focus state in a view tree is changed
Interfaceviewtreeobserver.ongloballayoutlistener
When the global layout changes in a view tree or the visual state of a view in the view tree changes, the interface class of the callback function to be called
Interfaceviewtreeobserver.onpredrawlistener
When a view tree is about to be drawn, the interface class of the callback function to invoke
Interfaceviewtreeobserver.onscrollchangedlistener
When some components in a view tree are scrolling, The interface class for the callback function to invoke
Interfaceviewtreeobserver.ontouchmodechangelistener
When the touch mode of a view tree changes, the interface class of the callback function to invoke
, We can use 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);        });


However, it is important to note that Ongloballayoutlistener may be triggered more than once, so after the height is obtained, the Ongloballayoutlistener is logged off.

The View post event gets

can also be obtained from the view's post method

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 (); int intheight = Textview.getmeasuredheight ();




Welcome to scan QR Code, follow public account



Android Get View height

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.