Android 如何获取按钮的高度和宽度
我已经创建了一系列按钮。现在我想找到按钮的高度和宽度,为此,我使用了Android 如何获取按钮的高度和宽度,android,Android,我已经创建了一系列按钮。现在我想找到按钮的高度和宽度,为此,我使用了getWidth()和getHeight()。但问题是它总是返回0。为什么会这样?我已经发送了我的代码,请检查是否有任何错误 LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout); LinearLayout rowLayout = null; LayoutParams param = new LinearLayout.LayoutPar
getWidth()
和getHeight()
。但问题是它总是返回0。为什么会这样?我已经发送了我的代码,请检查是否有任何错误
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
public static void main(String[] args)
{
DBFacade dbFacade = new DBFacade();
dbFacade.pick();
}
//Create Button
for(int i=0;i<6;i++)
{
rowLayout=new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout,param);
for(int j=0;j<7;j++)
{
m_pBtnDay[i][j]=new Button(this);
rowLayout.addView(m_pBtnDay[i][j],param);
m_pBtnDay[i][j].setOnLongClickListener(this);
m_pBtnDay[i][j].setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
m_pBtnDay[i][j].setTextSize(12);
}
}
x=m_pBtnDay[i][j].getWidth();
y=m_pBtnDay[i][j].getHeight();
Log.d("width",Integer.toString(x));
Log.d("Height",Integer.toString(y));
return true;
LinearLayout layout vertical=(LinearLayout)findViewById(R.id.liVLayout);
LinearLayout rowLayout=null;
LayoutParams param=新的LinearLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.FILL\u父级,1);
公共静态void main(字符串[]args)
{
DBFacade DBFacade=新的DBFacade();
dbFacade.pick();
}
//创建按钮
对于(int i=0;i可能您调用getWidth()
和getHeight()
太早了:我认为UI还没有在屏幕上调整大小和布局…
您可以尝试将该代码放在以下内容中:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
// Call here getWidth() and getHeight()
}
另一种方式
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
width = button.getWidth();
height = button.getHeight();
}
});
您尝试使用计数变量“i”和“j”从两个for循环外部使用。这将引发一个异常,因为每个for块中仅可用
尝试在第二个for循环中生成输出
for (int i = 0; i<6; i++)
{
...
for(int j=0; j<7; j++)
{
....
x = m_pBtnDay[i][j].getWidth();
y = m_pBtnDay[i][j].getHeight();
Log.d("width", Integer.toString(x));
Log.d("Height", Integer.toString(y));
}
}
for(inti=0;i将其放入循环中
x = m_pBtnDay[i][j].getWidth();
y = m_pBtnDay[i][j].getHeight();
Log.d("width", Integer.toString(x));
Log.d("Height", Integer.toString(y));
试试它试试这个也可以:
btn.getDrawingCache().getHeight() and btn.getDrawingCache().getWidth()
可以替代在布局阶段调用的以下视图方法
protected void onSizeChanged (int w, int h, int oldw, int oldh)
这似乎更合适,因为它明确说明了您观察到的零宽度和零高度:
当此视图的大小更改时,将在布局期间调用此函数。
如果您刚刚添加到视图层次结构,则会使用
旧值为0
参数w此视图的当前宽度。h此视图的当前高度
视图。此视图的旧宽度。此视图的旧高度
请参阅。因此,我应该何时调用这些函数。我已经看过许多示例,但无法得到答案。您能帮我解决这些问题吗out@Anshuman:在检查宽度和高度之前,创建一个动态布局并让它显示出来。在我编辑的代码中,我提供了一个示例,但我确信这不是唯一的一个…@Anshuman:你没有告诉我们在哪种情况下h函数/事件您正在调用代码…是的,当我在onWindowFocusChanged(boolean hasFocus)上编写getWidth和getHeight的代码并给出结果时。对于不同的分辨率,它会给出不同的宽度和高度吗?谢谢;这解决了问题。(调用方法太早)在场景中调用show
后,尝试设置getWidth