获得在Android的布局宽度/高度 [英] Getting the width/height of a layout in Android

查看:219
本文介绍了获得在Android的布局宽度/高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何来衡量一个视图的尺寸。在我的情况是AAN绝对布局。我读过关于这些问题的答案,但我还是不明白这一点。我是相当新的编程,所以也许这只是我。 ;)

I'm wondering how to measure the dimensions of a view. In my case it is aan Absolute Layout. I've read the answers concerning those questions but I still don't get it. I'm fairly new to programming so maybe it's just me. ;)

这是我的code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);      

    AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);       

    drawOval(); 

}
public void drawOval(){ //, int screenWidth, int screenHeight){ 
    AbsoluteLayout layoutbase = (AbsoluteLayout) findViewById(R.id.layoutbase);     

    int screenWidth = layoutbase.getWidth();
    int screenHeight = layoutbase.getHeight();
    Log.i("MyActivity", "screenWidth: " + screenWidth + ", screenHeight: " +screenHeight);

    Coordinates c = new Coordinates(BUTTONSIZE,screenWidth,screenHeight);

    ...some code ...

    ((ViewGroup) layoutbase ).addView(mybutton, new AbsoluteLayout.LayoutParams(BUTTONSIZE, BUTTONSIZE, c.mX, c.mY));


    mybutton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showText(mybutton);
        }
    });
}

public void showText(View button){      

    int x = findViewById(LAYOUT).getWidth();
    int y = findViewById(LAYOUT).getHeight(); 

    Toast message = Toast.makeText(this, "x: " + x , Toast.LENGTH_SHORT);       
    message.show();     


}

的getWidth()命令的伟大工程showText()但它的不可以 drawOval()。我知道这看起来有点不同的存在,但我还使用了 INT X = findViewById(布局).getWidth(); 版本 drawOval(),和X / Y总是 0 。我真的不明白,为什么有似乎没有宽/高在更早一点。即使我其实画一个按钮上的绝对布局,的getWidth()返回 0 。 Oviously我要测量的尺寸在 drawOval()

The getWidth() command works great in showText() but it does not in drawOval(). I know it looks a bit different there but I also used the int x = findViewById(LAYOUT).getWidth(); version in drawOval(), and x/y are always 0. I don't really understand why there seems to be no width/height at that earlier point. Even if I actually draw a Button on the Absolute Layout, getWidth() returns 0. Oviously I want to measure the sizes in drawOval().

我会AP preciate任何帮助。

I would appreciate any help.

谢谢!

推荐答案

我想会帮助你。

   LinearLayout headerLayout = (LinearLayout)findviewbyid(R.id.headerLayout);
    ViewTreeObserver observer = headerLayout .getViewTreeObserver();
            observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            int headerLayoutHeight= l.getHeight();
        int headerLayoutWidth = l.getWidth();
        headerLayout .getViewTreeObserver().removeGlobalOnLayoutListener(
                this);
    }
});


}  

这篇关于获得在Android的布局宽度/高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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