【问题标题】:button action does not work after rotation旋转后按钮操作不起作用
【发布时间】:2012-03-21 22:23:32
【问题描述】:

我有一个带有一些按钮的相对布局,我创建了一个旋转动画来旋转它

但是当我旋转它时,按钮的动作不能正常工作,按钮的动作仍然保存旋转前按钮的旧位置

谁能帮我解决这个问题

    <ImageButton
        android:id="@+id/last20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:background="@null"
        android:src="@drawable/last20_selector" 
        android:onClick="last20OnClick"/>

super.onCreate(savedInstanceState);
    setContentView(R.layout.wheel);

    RelativeLayout layout =  (RelativeLayout) findViewById(R.id.wheelLayout);

    RotateAnimation rotateAnim = new RotateAnimation(0, 45,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
            0.5f);

    rotateAnim.setDuration(1000);
    rotateAnim.setRepeatCount(0);
    rotateAnim.setFillAfter(true);
    layout.startAnimation(rotateAnim);
}

public void last20OnClick(View view) {
        System.out.println("last20OnClick");
    }

【问题讨论】:

  • 你找到解决办法了吗?面临同样的问题。
  • 不,我没有找到解决方案,我使用画布构建控件并停止使用绝对布局。

标签: android android-layout android-button


【解决方案1】:

您在代码中的哪个位置设置了OnClickListener? 尝试将监听器放在你的onCreate 方法中:

findViewById(R.id.last20).setOnClickListener(new OnClickListener() {

    public abstract void onClick (View v) {
        last20OnClick(v);
    }
});

希望对你有帮助!

【讨论】:

  • However, instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. developer.android.com/reference/android/widget/Button.html
  • 是的,我已经在 XML 文件 android:onClick 中设置了
【解决方案2】:

尝试设置属性:

android:fillAfter="false"
android:fillBefore="false"

或者在java中:

rotateAnim.setFillAfter(false);
rotateAnim.setFillBefore(false);

正如question 中所建议的那样。它帮助了我。 此外,为了保留您的动画,请从 -45 度旋转到 0 度。

【讨论】:

  • 不,我不能这样做,因为我需要保持轮换
  • RotateAnimation rotateAnim = new RotateAnimation(-45, 0, ...);你得到了同样的旋转。
猜你喜欢
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-04
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
相关资源
最近更新 更多