71

我正在构建一个 UI,它都是在 XML 中定义的静态内容。所有的东西都有重量,虽然它看起来不错,但我想看看这些东西实际上有正确的高度等等。问题是,无论我在哪里为格式布局调用 .getHeight(),我都得到了 0。我在 onCreate() 和 onStart() 中都进行了尝试。一样。也适用于所有 UI 对象。任何的想法?

package com.app.conekta;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class Conekta extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

    }

    @Override
    public void onStart() {

        super.onStart();


    }

    @Override
    public void onResume() {
        super.onResume();

        FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout);
        FrameLayout fl2 = (FrameLayout) findViewById(R.id.footerFrameLayout);
        Button b=(Button) findViewById(R.id.searchButton);

        Log.d("CONEKTA", String.valueOf(b.getHeight()));

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <FrameLayout
        android:id="@+id/headerFrameLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:background="#597eAA" >

        <ImageView
            android:id="@+id/logoImage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#7ba1d1"
            android:src="@drawable/logo_conekta" />
    </FrameLayout>

    <LinearLayout
        android:id="@+id/bodyLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.7"
        android:background="#f3f3f3"
        android:orientation="horizontal" >

        <FrameLayout
            android:id="@+id/leftBlankFrameLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.15"
            android:background="#f3f3f3" >
        </FrameLayout>

        <LinearLayout
            android:id="@+id/centerVerticalLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.7"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@+id/topCenterFrameLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.35" >
            </FrameLayout>

            <TextView
                android:id="@+id/venueLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.025"
                android:text="What are you looking for?"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000" />

            <EditText
                android:id="@+id/venueTextField"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.025" >

                <requestFocus />
            </EditText>

            <FrameLayout
                android:id="@+id/middleCenterFrameLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.05" >
            </FrameLayout>

            <TextView
                android:id="@+id/locationLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.025"
                android:text="Where?"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000000" />

            <AutoCompleteTextView
                android:id="@+id/locationTextField"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.025"
                android:text="" />

            <LinearLayout
                android:id="@+id/buttonLinearLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.05"
                android:background="#f3f3f3"
                android:orientation="horizontal" >

                <FrameLayout
                    android:id="@+id/leftButtonLinearLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.1" >
                </FrameLayout>

                <Button
                    android:id="@+id/searchButton"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.8"
                    android:background="#6fa8dc"
                    android:text="Search" />

                <FrameLayout
                    android:id="@+id/rightButtonLinearLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="0.1" >
                </FrameLayout>
            </LinearLayout>

            <FrameLayout
                android:id="@+id/bottomCenterFrameLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.35" >
            </FrameLayout>
        </LinearLayout>

        <FrameLayout
            android:id="@+id/rightBlankFrameLayout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="0.15"
            android:background="#f3f3f3" >
        </FrameLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/footerFrameLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:background="#7ba1d1" >
    </FrameLayout>

</LinearLayout>
4

6 回答 6

90

它是 0,因为在 onCreate 和 onStart 中,实际上还没有绘制视图。您可以通过侦听实际绘制视图的时间来解决此问题:

final TextView tv = (TextView)findViewById(R.id.venueLabel);
final ViewTreeObserver observer= tv.getViewTreeObserver();
       observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
              tv.getHeight()
              observer.removeGlobalOnLayoutListener(this);
            }
        });

删除侦听器的调用是为了防止在布局更改时重复调用您的自定义处理程序......如果您想获得这些,您可以省略它。

于 2011-11-17T16:48:49.590 回答
42

简而言之,视图尚未在 onCreate()、onStart() 或 onResume() 中构建。由于它们在技术上不存在(就 ViewGroup 而言),它们的尺寸为 0。

总而言之,您可以到这里获得有关如何处理它的更好解释。

如何检索视图的尺寸?

于 2011-11-17T16:48:20.820 回答
18

使用此函数获取视图的高度或宽度

private int getHeightOfView(View contentview) {
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    //contentview.getMeasuredWidth();
    return contentview.getMeasuredHeight();
}
于 2017-08-30T12:50:16.247 回答
5

我再次回答这个问题,以确保像我这样的人在实施之前能够完全理解它是如何工作的。

问:为什么我的视图高度为 0?
A.因为您尝试获取的视图高度尚未内置在 onCreate()、onStart() 或 onResume() 中。

问:现在我应该在哪里以及如何获得视图的高度?
A.您仍然可以在任意位置获取视图的参数,例如 onCreate()、onStart() 或 onResume()。如下所示:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       TextView tv = (TextView)findViewById(R.id.textview1);

       ViewTreeObserver viewTreeObserver = tv.getViewTreeObserver();
       if (viewTreeObserver.isAlive()) {
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                viewHeight = tv.getHeight();

            }
        });
      }
   }

但是,这一次您将等到获得视图的高度后再移除观察者。

tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

问:那么在删除此侦听器之前我应该​​检查什么?
**A.**只需在删除 treeObserver 之前添加一个 if 条件,例如,

 if (viewHeight != 0)                     
 tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

最终代码:

ViewTreeObserver viewTreeObserver = tv.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    int viewHeight = tv.getHeight();
                    if (viewHeight != 0)
                        tv.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                }
            });
        }

希望这能解释为什么 getHeight 返回 0 以及如何正确获取它。谢谢。

于 2018-07-18T08:02:32.860 回答
1

通过在视图中获取按钮,您可以轻松获取其高度等。您可以定义onClickListener如下:

Button b=(Button) findViewById(R.id.searchButton);
b.setOnClickListener(ButtonClick);
android.view.View.OnClickListener ButtonClick= new android.view.View.OnClickListener() {
    public void onClick(View v) { 
 v.getHeight();
}};

希望这可以帮助。

于 2012-06-23T12:44:57.980 回答
0

我遇到过这个问题,鉴于没有人提到这个解决方案,我觉得分享一下很有用。

您可以@Override使用该onSizeChange方法,我猜这发生在通货膨胀过程中。

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    getDimensions();
    
}

wheregetDimensions()是您的 custom 方法,您可以View在其中调用getHeightgetWidth分配给您的类字段并根据需要重新设置。

于 2021-03-21T19:43:49.510 回答