Android RelativeLayout getWidth返回零

Android RelativeLayout getWidth返回零,android,Android,首先,我知道这是另一个问题的重复,但我无法解决它,所以我创建了自己的问题以寻求帮助。我正在尝试获取RelativeLayout宽度,起初它是成功的,但我不知道我更改了什么,代码只返回零 这是我的代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

首先,我知道这是另一个问题的重复,但我无法解决它,所以我创建了自己的问题以寻求帮助。我正在尝试获取
RelativeLayout
宽度,起初它是成功的,但我不知道我更改了什么,代码只返回零

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView[] imageViewArray = new ImageView[20];
    ArrayList<Float> xarray = new ArrayList<>();
    ArrayList<Float> yarray = new ArrayList<>();
    rlt = (RelativeLayout) findViewById(R.id.layout);
    imageView = (ImageView) findViewById(R.id.imageView);
    rlt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rlt.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            Toast.makeText(MainActivity.this, "right" + rlt.getWidth(), Toast.LENGTH_SHORT).
                    show();
            layoutwidth = rlt.getWidth();
            layoutheight = rlt.getHeight();
        }
    });
}
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/layout"
    android:background="@android:color/black"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/you"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView[]imageViewArray=新的ImageView[20];
ArrayList xarray=新的ArrayList();
ArrayList yarray=新的ArrayList();
rlt=(相对)findViewById(R.id.layout);
imageView=(imageView)findViewById(R.id.imageView);
rlt.getViewTreeObserver().addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener()){
@凌驾
公共图书馆{
rlt.getViewTreeObserver();
Toast.makeText(MainActivity.this,“right”+rlt.getWidth(),Toast.LENGTH\u SHORT)。
show();
layoutwidth=rlt.getWidth();
layouthweight=rlt.getHeight();
}
});
}
这是我的活动\u主xml文件:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView[] imageViewArray = new ImageView[20];
    ArrayList<Float> xarray = new ArrayList<>();
    ArrayList<Float> yarray = new ArrayList<>();
    rlt = (RelativeLayout) findViewById(R.id.layout);
    imageView = (ImageView) findViewById(R.id.imageView);
    rlt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rlt.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            Toast.makeText(MainActivity.this, "right" + rlt.getWidth(), Toast.LENGTH_SHORT).
                    show();
            layoutwidth = rlt.getWidth();
            layoutheight = rlt.getHeight();
        }
    });
}
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/layout"
    android:background="@android:color/black"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/you"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>


简单的一个,但不工作,有什么帮助吗

这是因为布局的宽度尚未设置 您应该重写onLayout()方法


通常,您会扩展您感兴趣的任何
视图
(此处为
RelativeLayout
),并覆盖
onSizeChanged()

这在您的场景中是否有用很难说,因为您的问题相当笼统。

试试看

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run()
    {
        //get width here
    }
}, 10);

也许可以尝试将视图的代码放入“onPostCreate”方法中

从一个重复的问题看这篇文章,这个问题在给出的答案中非常全面:你使用Android Studio吗?也许你可以在当地的历史中找到它?在活动文件->本地历史->显示历史中单击鼠标右键。调用
super.onLayout(…)
后,将计算布局的宽度和高度,允许您调用
rlt.getWidth()
正如您之前所期望的,实际上我做了一些调试,并注意到只有在我在onGlobalLayout侦听器外部使用layoutwidth时,该值才返回零,当前代码正在工作,它实际返回宽度,但我需要在外部使用它,如果我将Toast移动到onGlobalLayout之后,它将返回零。我不知道为什么。无论如何,thx.@GuyBalas,因此您可以将这些值存储在局部变量中,并通过getters访问。这并不总是有效的,只是将“run”方法中的任何内容的执行延迟10毫秒。