视图的 getWidth() 和 getHeight() 返回 0 [英] View's getWidth() and getHeight() returns 0

查看:21
本文介绍了视图的 getWidth() 和 getHeight() 返回 0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建我的 android 项目中的所有元素.我正在尝试获取按钮的宽度和高度,以便我可以旋转该按钮.我只是想学习如何使用 android 语言.但是,它返回 0.

我做了一些研究,我发现它需要在 onCreate() 方法之外的其他地方完成.如果有人能给我一个如何做的例子,那就太好了.

这是我当前的代码:

package com.animation;导入 android.app.Activity;导入 android.os.Bundle;导入 android.view.animation.Animation;导入 android.view.animation.LinearInterpolator;导入 android.view.animation.RotateAnimation;导入 android.widget.Button;导入 android.widget.LinearLayout;公共类 AnimateScreen 扩展 Activity {//在第一次创建活动时调用.@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LinearLayout ll = new LinearLayout(this);LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);layoutParams.setMargins(30, 20, 30, 0);Button bt = new Button(this);bt.setText(String.valueOf(bt.getWidth()));RotateAnimation ra = new RotateAnimation(0,360,bt.getWidth()/2,bt.getHeight()/2);ra.setDuration(3000L);ra.setRepeatMode(Animation.RESTART);ra.setRepeatCount(Animation.INFINITE);ra.setInterpolator(new LinearInterpolator());bt.startAnimation(ra);ll.addView(bt,layoutParams);setContentView(ll);}

感谢任何帮助.

解决方案

您调用 getWidth() 太早了.UI 尚未在屏幕上调整大小和布局.

无论如何,我怀疑您是否想做您正在做的事情——正在制作动画的小部件不会改变它们的可点击区域,因此无论按钮如何旋转,按钮仍会响应原始方向的点击.

话虽如此,您可以使用维度资源 定义按钮大小,然后从您的布局文件和源代码中引用该尺寸资源,以避免此问题.

I am creating all of the elements in my android project dynamically. I am trying to get the width and height of a button so that I can rotate that button around. I am just trying to learn how to work with the android language. However, it returns 0.

I did some research and I saw that it needs to be done somewhere other than in the onCreate() method. If someone can give me an example of how to do it, that would be great.

Here is my current code:

package com.animation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;

public class AnimateScreen extends Activity {


//Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout ll = new LinearLayout(this);

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(30, 20, 30, 0);

    Button bt = new Button(this);
    bt.setText(String.valueOf(bt.getWidth()));

    RotateAnimation ra = new RotateAnimation(0,360,bt.getWidth() / 2,bt.getHeight() / 2);
    ra.setDuration(3000L);
    ra.setRepeatMode(Animation.RESTART);
    ra.setRepeatCount(Animation.INFINITE);
    ra.setInterpolator(new LinearInterpolator());

    bt.startAnimation(ra);

    ll.addView(bt,layoutParams);

    setContentView(ll);
}

Any help is appreciated.

解决方案

You are calling getWidth() too early. The UI has not been sized and laid out on the screen yet.

I doubt you want to be doing what you are doing, anyway -- widgets being animated do not change their clickable areas, and so the button will still respond to clicks in the original orientation regardless of how it has rotated.

That being said, you can use a dimension resource to define the button size, then reference that dimension resource from your layout file and your source code, to avoid this problem.

这篇关于视图的 getWidth() 和 getHeight() 返回 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆