你如何检索视图的尺寸是多少? getHeight()都会和的getWidth()总是返回零 [英] How do you to retrieve dimensions of a view? getHeight() and getWidth() always return zero
问题描述
我已经做了tablelayout的景色,tablrows和textviews看起来像一个网格。我需要得到这个网格的高度和宽度。该方法的getHeight()和的getWidth()总是返回0。这发生在我动态格式的网格或使用XML版本。
可能有人请告知如何检索维度的看法?
下面是我的测试程序我用调试检查结果:
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TableLayout;
进口android.widget.TextView;
公共类appwig延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.maindemo); //< - 包括所谓的板网格
INT VH = 0;
INT VW = 0;
//试验1中使用的XML布局(其被显示在屏幕上):
TableLayout TL =(TableLayout)findViewById(R.id.board);
TL =(TableLayout)findViewById(R.id.board);
VH = tl.getHeight(); //< - 的getHeight返回0,为什么呢?
VW = tl.getWidth(); //< - 的getWidth返回0,为什么呢?
//测试-2用一个简单的动态生成的观点:
TextView的电视=新的TextView(本);
tv.setHeight(20);
tv.setWidth(20);
VH = tv.getHeight(); //< - 的getHeight返回0,为什么呢?
VW = tv.getWidth(); //< - 的getWidth返回0,为什么呢?
} // EOF方法
} // EOF类
我相信OP早已不复存在,但如果这个答案是能够帮助未来的搜索,我想我会后,我已经找到了解决办法。我加入了这个code到我的的onCreate()
方法:
编辑: 07/05/11包括来自评论code:
最后的TextView电视=(TextView中)findViewById(R.id.image_test);
ViewTreeObserver VTO = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
LayerDrawable LD =(LayerDrawable)tv.getBackground();
ld.setLayerInset(1,0,tv.getHeight()/ 2,0,0);
ViewTreeObserver观测值= tv.getViewTreeObserver();
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.JELLY_BEAN){
obs.removeOnGlobalLayoutListener(本);
} 其他 {
obs.removeGlobalOnLayoutListener(本);
}
}
});
首先,我得到一个最终参考我的的TextView
(在 onGlobalLayout()
方法访问)。接下来,我得到的 ViewTreeObserver
从我的的TextView
,并添加 OnGlobalLayoutListener
,覆盖 onGLobalLayout
(似乎没有成为一个超类方法来调用这里...),并加入我的code这需要知道的测量查看到这个监听器。所有按预期工作对我来说,我希望这能帮助。
I have a view made up of tablelayout, tablrows and textviews to look like a grid. I need to get the height and width of this grid. The methods getheight() and getwidth() always return 0. This happens when I format the grid dynamically or use an xml version.
Could someone please tell how to retrieve the dimensions for a view?
Here is my test program I used in Debug to check the results:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TableLayout;
import android.widget.TextView;
public class appwig extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maindemo); //<- includes the grid called "board"
int vh = 0;
int vw = 0;
//Test-1 used the xml layout (which is displayed on the screen):
TableLayout tl = (TableLayout) findViewById(R.id.board);
tl = (TableLayout) findViewById(R.id.board);
vh = tl.getHeight(); //<- getHeight returned 0, Why?
vw = tl.getWidth(); //<- getWidth returned 0, Why?
//Test-2 used a simple dynamically generated view:
TextView tv = new TextView(this);
tv.setHeight(20);
tv.setWidth(20);
vh = tv.getHeight(); //<- getHeight returned 0, Why?
vw = tv.getWidth(); //<- getWidth returned 0, Why?
} //eof method
} //eof class
I believe the OP is long gone, but in case this answer is able to help future searchers, I thought I'd post a solution that I have found. I have added this code into my onCreate()
method:
EDITED: 07/05/11 to include code from comments:
final TextView tv = (TextView)findViewById(R.id.image_test);
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
LayerDrawable ld = (LayerDrawable)tv.getBackground();
ld.setLayerInset(1, 0, tv.getHeight() / 2, 0, 0);
ViewTreeObserver obs = tv.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
First I get a final reference to my TextView
(to access in the onGlobalLayout()
method). Next, I get the ViewTreeObserver
from my TextView
, and add an OnGlobalLayoutListener
, overriding onGLobalLayout
(there does not seem to be a superclass method to invoke here...) and adding my code which requires knowing the measurements of the view into this listener. All works as expected for me, so I hope that this is able to help.
这篇关于你如何检索视图的尺寸是多少? getHeight()都会和的getWidth()总是返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!