【问题标题】:android: get viewHeight programmaticallyandroid:以编程方式获取视图高度
【发布时间】:2011-06-10 09:54:27
【问题描述】:

我正在尝试获取视图的高度,但它一直返回 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);
    }

【问题讨论】:

    标签: android


    【解决方案1】:

    您提供的高度为wrap_content,但您没有填写该内容。因此,它将显示为 0。

    【讨论】:

    • 好吧,我尝试在 100dip 之类的东西上指定高度,但它仍然返回 0
    • 你是在指定线性布局的高度吗?
    • im 使用 relativelayout,如上面的 xml 代码所示。我自己使用 android:layout_height="50dip" 指定了高度,但仍然无法正常工作。我还使用粘贴在 onStart 中的 OnGlobalLayoutListener 进行了尝试,甚至没有工作。为什么仅仅返回视图高度就这么辛苦!
    • 它是 wrap_content,带有一个包含文本的子 TextView,设置为 wrap_content。测量后它尺寸。
    【解决方案2】:

    试试

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

    【讨论】:

    • 我认为.measure 方法在 GlobalLayoutListener 中是不必要的,但我相信这是使用 getMeasuredHeight() 和 getMeasuredWidth() 而不是标准 getHeight () 和 getWidth()。
    【解决方案3】:

    在第一个示例中它不起作用,因为当您查询它们时,视图仍然没有执行布局和测量步骤。你只是告诉视图它在布局中的“行为”,但它仍然没有计算出每个视图的放置位置。

    (类似问题:How to get the width and height of an android.widget.ImageView?

    在您发布的第二个示例中,我认为您没有将视图添加到活动中,因此活动不会绘制它,因此您将永远不会阅读该日志消息。调用 setContentViewaddView 到一些布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 1970-01-01
      • 2014-02-22
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多