怎么在Activity中获取指定控件的宽高
2016-04-22 20:20
211 查看
第一种方法:
第二种方法:
下面是全部代码:
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);
}
});
}
}
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);
}
});
}
}
相关文章推荐
- JavaScript Window - 浏览器对象模型
- LinkedList学习笔记
- markdown pieces
- 十种经典算法
- 【bzoj 1057】[ZJOI2007] 棋盘制作
- iOSApp上架教程
- Json的JsonValueProcessor方法
- 访问控制
- poj 3070 java实现矩阵快速幂
- CI框架局部缓存
- 手机在 Android studio 中不显示logcat信息
- hdu 2686(多线程dp)
- Bestcoder Pro.ID 2012 素数判定
- LeetCode 70. Climbing Stairs
- POJ 1328 Radar Installation
- 我对Android的认识
- 大数据基础
- 大数据基础
- android学习之绑定Service并与之通信
- 进程死锁条件和银行家算法