如何获取的高度和宽度巴顿 [英] How to get height and width of Button

查看:132
本文介绍了如何获取的高度和宽度巴顿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建按钮阵列。现在,我想找到按钮的高度和宽度,并为我用的getWidth()和getHeight()。但事实是,它总是返回0。这究竟是为什么?我送我的code,请检查什么是错的。

  INT X,Y;
    的LinearLayout layoutVertical =(的LinearLayout)findViewById(R.id.liVLayout);
    LinearLayout中RowLayout的= NULL;

    的LayoutParams参数=新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1);

    //创建按钮
    的for(int i = 0; I< 6,我++)
    {
        RowLayout的=新的LinearLayout(本);
        rowLayout.setWeightSum(7);
        layoutVertical.addView(RowLayout的,参数);

        为(诠释J = 0; J&小于7; J ++)
        {
            m_pBtnDay [I] [J] =新的按钮(这一点);

            rowLayout.addView(m_pBtnDay [I] [J],参数);

            m_pBtnDay [I] [J] .setOnLongClickListener(本);

            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(宽度,Integer.toString(X));
    Log.d(高度,Integer.toString(Y));
    返回true;
 

解决方案

也许你正在调用的getWidth()的getHeight()太早:我觉得用户界面一直没有大小和布局在屏幕上尚未...
你可以尝试把那个code这里面:

  @覆盖
公共无效onWindowFocusChanged(布尔hasFocus){
    super.onWindowFocusChanged(hasFocus);
    //调用这里的getWidth()和getHeight()
 }
 

I have created an array of buttons. Now I want to find the height and width of the button, and for that i have used getWidth() and getHeight(). But the thing is that it always return 0. Why is this happening? I have send my code, please check if anything is wrong.

    int x,y;
    LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
    LinearLayout rowLayout = null;

    LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);

    //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;

解决方案

Probably you are calling getWidth() and getHeight() too early: I think the UI has not been sized and laid out on the screen yet...
You can try to put that code inside this:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    // Call here getWidth() and getHeight()
 }

这篇关于如何获取的高度和宽度巴顿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆