在android运行时获取布局高度和宽度



如何获得在xml中定义为fill_prent的线性布局的宽度和高度?我试过一种测量方法,但我不知道为什么它不能给出确切的值。在oncreate方法完成之前,我需要在"活动"中使用这些值。

假设我必须获得用XML定义的LinearLayout宽度。我必须通过XML获得它的引用。将LinearLayout l定义为实例。

 l = (LinearLayout)findviewbyid(R.id.l1);
ViewTreeObserver observer = l.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                init();
            l.getViewTreeObserver().removeGlobalOnLayoutListener(
                    this);
        }
    });
protected void init() {
        int a= l.getHeight();
            int b = l.getWidth();
Toast.makeText(getActivity,""+a+" "+b,3000).show();
    } 
    callfragment();
}  

宽度和高度值是在创建布局后设置的,当放置元素时,将对其进行测量。在第一次调用onSizeChanged时,parms将为0,因此如果您对其进行检查。

此处提供更多细节https://groups.google.com/forum/?fromgroups=#!主题/安卓开发者/nNEp6xBnPiw

在这里http://developer.android.com/reference/android/view/View.html#Layout

以下是如何使用onLayout:

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int width = someView.getWidth();
    int height = someView.getHeight();
}

要使其工作,您需要检查所需的高度值是否大于0,然后首先删除onGlobalLayout侦听器,并对高度执行任何操作。侦听器连续调用其方法,并且通过第一次调用不能保证视图得到正确测量。

    final LinearLayout parent = (LinearLayout) findViewById(R.id.parentView);
    parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int availableHeight = parent.getMeasuredHeight();
            if(availableHeight>0) {
                parent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                //save height here and do whatever you want with it
            }
        }
    });

您可以在布局中添加布局更改侦听器,并获得最新的高度和宽度,甚至是上次更改之前的高度和广度。

加入API 11级

添加一个侦听器,该侦听器将在视图的边界更改时调用由于布局处理。

LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.my_linear_layout);
myLinearLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            // Preventing extra work because method will be called many times.
            if(height == (bottom - top))
                return;
            
            height = (bottom - top);
            // do something here...
           }
     });

一种基于MGDroid对API 16+的回答使用Kotlin的通用方法。

/**
 * Align height of a container from wrap-content to actual height at runtime.
 * */
private fun <T: ViewGroup> alignContainerHeight(container: T) {
    container.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        override fun onGlobalLayout() {
            // Obtain runtime height
            val availableHeight = container.measuredHeight
            if (availableHeight > 0) {
                container.viewTreeObserver.removeOnGlobalLayoutListener(this)
                setContainerHeight(container, availableHeight)
            }
        }
    })
}
/**
 * Note: Assumes that the parent is a LinearLayout.
 * */
private fun <T : ViewGroup> setContainerHeight(container: T, availableHeight: Int) {
    val availableWidth = container.measuredWidth
    val params = LinearLayout.LayoutParams(availableWidth, availableHeight)
    // Note: getLayoutParams() returns null if no parent exists
    if (container.layoutParams != null) {
        container.layoutParams = params
    }
}

最新更新