欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android开发如何动态设置控件等宽高

程序员文章站 2022-03-23 20:41:31
Android开发如何动态设置控件等宽高。 public static void setViewWidthByHeight(View view) { final...

Android开发如何动态设置控件等宽高。

public static void setViewWidthByHeight(View view) {  
    final View mv = view;  
    final ViewTreeObserver vto = mv.getViewTreeObserver();  
    final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() {  
        public boolean onPreDraw() {  

/               int width = mv.getMeasuredWidth();  
            int height = mv.getMeasuredHeight();  

            android.view.ViewGroup.LayoutParams lp = mv.getLayoutParams();  
            lp.width = height;  
            mv.setLayoutParams(lp);  

            final ViewTreeObserver vto1 = mv.getViewTreeObserver();  
            vto1.removeOnPreDrawListener(this);  

            return true;  
        }  
    };  
    vto.addOnPreDrawListener(preDrawListener);  
    }