
我动态地在我的android项目中创建所有元素.我试图获取按钮的宽度和高度,以便我可以旋转该按钮.我只是想学习如何使用androID语言.但是,它返回0.
我做了一些研究,我发现它需要在onCreate()方法之外的某个地方完成.如果有人能给我一个如何做的例子,那就太好了.
这是我目前的代码:
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.@OverrIDepublic 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尚未调整大小并在屏幕上显示.
我怀疑你想要做你正在做的事情,无论如何 – 动画小部件不会改变他们的可点击区域,因此按钮仍然会响应原始方向的点击,无论它如何旋转.
话虽这么说,您可以使用dimension resource来定义按钮大小,然后从布局文件和源代码中引用该维度资源,以避免此问题.
总结以上是内存溢出为你收集整理的java – View的getWidth()和getHeight()返回0全部内容,希望文章能够帮你解决java – View的getWidth()和getHeight()返回0所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)