java - getHeight 和 getMeasuredHeight 返回 0,即使在 Runnable 中也是如此

标签 java android return runnable measurement

所以我正在搜索抛出所有 getHeight/widht 问题帖子,并尝试不同的方法来解决问题(如 globalLayoutListener 或 Runnable),等待,直到 FrameLayout 被创建......但仍然是我的getHeight 返回 0。 所以这是我现在的代码:

private void zurechtruecken() {
    int n=1;

    spB =(FrameLayout) findViewById(R.id.spielbereich);

    spB.post(new Runnable(){
        @Override
        public void run(){

            spielBh=spB.getMeasuredHeight();
            spielBb=spB.getMeasuredWidth();
        }
    });

这是我使用 GlobalLayoutListener 的代码:

private void zurechtruecken() {
    int i=24;
    int n=1;

    mView = findViewById(R.id.spielbereich);
    mView.getViewTreeObserver().addOnGlobalLayoutListener( 
        new OnGlobalLayoutListener(){

            @Override
            public void onGlobalLayout() {
                mView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                spielBh=mView.getHeight();
                spielBb=mView.getWidth();
                Log.i("!",Integer.toString(spielBh));
                Log.i("!",Integer.toString(spielBb));


            }
        });

“removeGlobalLayoutListener(this)”被交叉。(已弃用)

我测量框架布局可能很重要

<小时/>

感谢您的帮助! 波蒂560

最佳答案

我以前也遇到过这样的麻烦。如果所有其他方法都失败,请在您自己的类中扩展 FrameLayout 并在 onMeasure 方法中获取高度/宽度值。示例:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int height = MeasureSpec.getSize(heightMeasureSpec);

}

也可以轻松地将其放入 XML 布局中,例如:

<com.yourdomain.yourappname.FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > ...

关于java - getHeight 和 getMeasuredHeight 返回 0,即使在 Runnable 中也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27342571/

相关文章:

java - 如何迭代 HashMap<String, Set<String>> 并返回最大的 Set<String> ?

Java while循环不重复

android - 网站的字体 cambria 不适用于任何 android 设备

javascript - 何时使用 return,返回的数据会怎样?

arrays - typescript :声明返回嵌套数组的函数

java - Jackson 将动态 json 转换为 map

java - Android 中如何验证用户输入?

android - Samsung Galaxy SII 密码错误问题

Android底部栏菜单onclick Action

c++ - EXIT_SUCCESS 和 EXIT_FAILURE。是一次编译到处运行还是一次编写到处编译?