简体   繁体   English

从布局获取宽度,高度

[英]get width, height from Layout

I need to get width and height of Linear layout, however I do not know when the Relative Layout is ready to provide me these two attributes. 我需要获取线性布局的宽度和高度,但是我不知道相对布局何时准备向我提供这两个属性。

but it only returns 0. 但它只会返回0。

my screen 我的萤幕 在此处输入图片说明

please check my code, i have no idea. 请检查我的代码,我不知道。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

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

    @Override
    protected void onResume() {
        super.onResume();
        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        int width = container.getWidth();
        int height = container.getWidth();

        Log.d("MainActivity onCreate", "width #" + width + ", height #" + height);
    }
}

and activity_main.xml layout 和activity_main.xml布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:background="#FFFFFFAA"
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</RelativeLayout>

how do i fix this? 我该如何解决?

Whenever you try to get dimensions on onResume() or on onCreate() method, it always returns 0 . 每当您尝试在onResume()onCreate()方法上获取尺寸时,它始终返回0 So you should use ViewTreeObserver 所以你应该使用ViewTreeObserver

ViewTreeObserver vto = container.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        container.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = container.getMeasuredHeight();
        finalWidth = container.getMeasuredWidth();
        return true;
    }
});

Also you can customize your View which will extends with LinearLayout and in onMeasure() method you can get width and height . 另外,您还可以自定义View ,它将随LinearLayout扩展,在onMeasure()方法中,您可以获得widthheight

First of all replace 首先更换

 int height = container.getWidth();

by 通过

 int height = container.getHeight();

container.getHeight() will return 0 (as well as container.getWidth() ) if it's not drawed yet. 如果尚未绘制, container.getHeight()将返回0 (以及container.getWidth() )。

Solution would be to get the height after your view is layouted: 解决方案是在布局视图后获取高度:

container.post(new Runnable(){
    public void run(){
     int height = container.getHeight();
    }
});

First, you are calling getWidth twice - for width and height. 首先,您两次调用getWidth-表示宽度和高度。
You can try it with measureSpecs, like this: 您可以使用measureSpecs进行尝试,如下所示:

int matchParentMeasureSpecWidth = View.MeasureSpec.makeMeasureSpec(((View) container.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
int matchParentMeasureSpecHeight = View.MeasureSpec.makeMeasureSpec((View) container.getParent()).getHeight(), View.MeasureSpec.EXACTLY);
v.measure(matchParentMeasureSpecWidth, matchParentMeasureSpecHeight);

int height = container.getMeasuredHeight();

You can try and mesaure it first. 您可以先尝试测量一下。 But make sure you do so only after every other view as been added beacuse your LinearLayout is depended on other views. 但是请确保仅在添加所有其他视图之后再执行此操作,因为LinearLayout依赖于其他视图。

LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();

Try this 尝试这个

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

    final LinearLayout container = (LinearLayout)findViewById(R.id.container);

            container.post(new Runnable()
            {

                @Override
                public void run()
                {
                    Log.i("TEST", "  Layout width : "+ container.getWidth() +"  Layout height "+ container.getHeight()) ;

                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM