如何检索视图的维度?我的看法是TableLayout, TableRow and TextView..我想让它看起来像个网格。我需要得到这个网格的高度和宽度。方法getHeight()和getWidth()始终返回0。当我动态地格式化网格时,以及当我使用XML版本时,都会发生这种情况。如何检索视图的维度?下面是我在Debug中用来检查结果的测试程序: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
3 回答
HUWWW
TA贡献1874条经验 获得超12个赞
@Overridepublic void onWindowFocusChanged (boolean hasFocus) { // the height will be set at this point int height = myEverySoTallView.getMeasuredHeight(); }
- 3 回答
- 0 关注
- 459 浏览
添加回答
举报
0/150
提交
取消