How to get programmatically width and height of Relative - Linear layout in android?

前端 未结 6 1653
礼貌的吻别
礼貌的吻别 2020-12-11 01:53

I required runtime dimensions of Relative/Linear Layout.

activity_home.xml:



        
6条回答
  •  抹茶落季
    2020-12-11 02:37

    we may not get correct height and wight of a view or layout as soon the activity is just created since the layout is not inflated completly

    so

    1.either you can let a runnable method triggered after some secs which in turn call a function that gets the width and height properties.

    2.we can use view tree obsever

     l = (RelativeLayout)findviewbyid(R.id.rl_cards_details_card_area);
    ViewTreeObserver observer = l.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                ActivityClassName.this.getProperties();
            l.getViewTreeObserver().removeGlobalOnLayoutListener(
                    this);
        }
    });
    void getProperties() {
        int a= l.getHeight();
            int b = l.getWidth();
        Toast.makeText(getActivity,""+a+" "+b,1000).show();
        }
    

提交回复
热议问题