Android:以编程方式获取视图高度

5

我正在尝试获取一个视图的高度,但它一直返回0。

这是我尝试过的方法:

View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();

我也尝试了 hourViewHeight = hourView.getHeight();,但是没有成功。

我在这里的 initialize() 方法中调用了这两行代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calendar_day_screen);

        Intent intent = getIntent();

        mDate = new DateTime(intent.getStringExtra("date"));
        circleIcon = (ImageView) findViewById(R.id.circle);

        mHoursLayout = (RelativeLayout) findViewById(R.id.dayLayout);

        TopBarUtil topBar = new TopBarUtil(getApplicationContext());

        circleIcon.setOnClickListener(topBar.onActionClickListener());

        mContext = getApplicationContext();



        initialize();
    }

这是我的xml布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:id="@+id/mainLayout" android:background="@layout/border">

    <TextView android:text="12am" android:id="@+id/hourTextView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</RelativeLayout>

我希望能获取上述XML布局的高度,但总是返回0。

编辑:我也尝试过以下方法:

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

         final View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null);
        ViewTreeObserver observer = hourView.getViewTreeObserver();

        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                hourView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                hourViewHeight = hourView.getHeight();
                Log.d(TAG, "hourViewHeight = " + hourViewHeight);
            }
        });

//      hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight();
//      Log.d(TAG, "hourViewHeight = " + hourViewHeight);
    }
3个回答

7

尝试

hourView.measure(hourView.getWidth(), hourView.getHeight());
hourView.getMeasuredHeight()

如果这是在GlobalLayoutListener中,我认为.measure方法是不必要的,但是我相信使用getMeasuredHeight()getMeasuredWidth()而不是标准的getHeight()getWidth()是正确的。 - Kevin Coppock

1
在第一个例子中,它没有起作用是因为当你查询它们时,视图仍然没有执行布局和测量步骤。你只告诉视图在布局中如何“行为”,但它仍然没有计算出每个视图放置的位置。
(类似问题:如何获取android.widget.ImageView的宽度和高度?
在你发布的第二个例子中,我认为你没有将视图添加到活动中,所以活动不会绘制它,因此你永远不会读取那个日志消息。调用setContentViewaddView来添加到某个布局中。

-2

您已将高度设置为wrap_content,但未填充该内容。因此,它将显示为0。


我尝试将高度指定为100dip左右的数值,但仍然返回0。 - Jono
您正在指定LinearLayout的高度? - chikka.anddev
我在使用如上所示的xml代码中的RelativeLayout。我使用android:layout_height="50dip"指定了高度,但仍然不起作用。我还尝试在onStart中使用OnGlobalLayoutListener,但那也没起作用。为什么只是返回视图高度这么费力呢! - Jono
它是wrap_content,带有一个包含文本的子TextView,设置为wrap_content。一旦被测量,它就有一个大小。 - Kevin Coppock

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接