怎么在Activity中获取指定控件的宽高

本文介绍了两种在Android Activity中获取指定视图控件宽高的方法,包括直接获取和在视图布局完成后再获取,适用于动态计算布局的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方法:

TextView<span style="font-family: Arial, Helvetica, sans-serif;">=new TextView(this);</span>
textview.getViewTreeObserver().addOnGlobalLayoutListener(new onGlobalLayoutListener(){
   public void onGlobalLayout(){
      textview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
      textview.getHeight();
      textview.getWidght();
}

})

第二种方法:

textview.post(new Runnable{

   public void run(){

        textview.getHeight();
}

})

下面是全部代码:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class TestLPActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_lp);
        final TextView tv = (TextView) findViewById(R.id.tv);

//        LayoutParams layoutParams = (LayoutParams) tv.getLayoutParams();
//        layoutParams.height = 200;
//        layoutParams.width = 200;
//        tv.setLayoutParams(layoutParams);
//        System.out.println("layoutParams.height:" + layoutParams.height + "layoutParams.width:"
//                + layoutParams.width);
//        tv.measure(0, 0);
//        int measuredHeight = tv.getMeasuredHeight();
//        int measuredWidth = tv.getMeasuredWidth();
//        System.out.println("measuredHeight:" + measuredHeight + "measuredWidth:" + measuredWidth);
//        tv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
//            @Override
//            public void onGlobalLayout() {
//                // 结论: 如果布局文件里宽高配置 match_parent, wrap_content, LayoutParams就是 -1, -2.
//                // 无论什么时候都是
//                System.out.println(tv.getHeight());
//                System.out.println(tv.getLayoutParams().width+" "+tv.getLayoutParams().height);
//                RelativeLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
//                lp.height = 500;
//                tv.setLayoutParams(lp);
//                System.out.println(tv.getLayoutParams().width+" "+tv.getLayoutParams().height);
//                tv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//            }
//        });
        tv.post(new Runnable() {
            @Override
            public void run() {
                System.out.println(tv.getHeight());
                System.out.println(tv.getLayoutParams().width+" "+tv.getLayoutParams().height);
                RelativeLayout.LayoutParams lp = (LayoutParams) tv.getLayoutParams();
                lp.height = 500;
                tv.setLayoutParams(lp);
                System.out.println(tv.getLayoutParams().width+" "+tv.getLayoutParams().height);
            }
        });
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值