如何在Android中获取textView的宽度 [英] how to get the width of textView in android
本文介绍了如何在Android中获取textView的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我正在使用的代码...
This is the code what i am using...
<TextView
android:id="@+id/textView"
android:layout_width="100dp"
android:layout_height="40dp"
android:text="AMIT KUMAR" />
这是一个Java代码.
and here is a java code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
Log.d("left", textView.getLeft() + " " + textView.getRight() + " "
+ textView.getWidth()+" "+textView.getHeight());
}
对于上述所有内容,我的输出均为0.
for all above i am getting the output 0.
所以我怎么能得到textView的宽度和高度.
so how can i get the width and height of textView.
推荐答案
尝试如下:
textView.setText("GetHeight");
textView.measure(0, 0); //must call measure!
textView.getMeasuredHeight(); //get height
textView.getMeasuredWidth(); //get width
这篇关于如何在Android中获取textView的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文