如何检索视图的尺寸? [英] How to retrieve the dimensions of a view?
问题描述
我有一个由TableLayout, TableRow and TextView
组成的视图.我希望它看起来像一个网格.我需要获取此网格的高度和宽度.方法getHeight()
和getWidth()
始终返回0.这在我动态格式化网格以及使用XML版本时会发生.
I have a view made up of TableLayout, TableRow and TextView
. I want it 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 and also when I use an XML version.
如何检索视图的尺寸?
How to retrieve the dimensions for a view?
这是我在Debug中用于检查结果的测试程序:
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
推荐答案
我相信OP已经不复存在了,但是如果此答案能够为将来的搜索者提供帮助,我想我应该发布一个找到的解决方案.我已将此代码添加到我的onCreate()
方法中:
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:
已 11年7月5日,包括注释中的代码:
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);
}
}
});
首先,我获得了对我的TextView
的最终引用(要在onGlobalLayout()
方法中进行访问).接下来,我从TextView
中获取ViewTreeObserver
,并添加一个OnGlobalLayoutListener
,覆盖onGLobalLayout
(似乎不是在这里调用的超类方法...)并添加需要了解查看此侦听器的视图的大小.一切都按我的预期进行,因此希望对您有所帮助.
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.
这篇关于如何检索视图的尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!