我想在Android的xml布局文件上显示一些UI元素。我正在制作一个应用程序,两个玩家可以坐在移动设备的两端,互相对抗。
因此需要显示一些旋转了180度的按钮。
这是可能的吗?我尝试过android:gravity,但这没有起作用。
谢谢你的帮助, 马丁
因此需要显示一些旋转了180度的按钮。
这是可能的吗?我尝试过android:gravity,但这没有起作用。
谢谢你的帮助, 马丁
我建议您查看此线程,该线程讨论了类似的问题。虽然该问题是关于TextView组件的,但Button扩展了TextView,因此将其适应到按钮上应该很容易。该问题的提问者最终采用了以下onDraw()
方法:
@Override
public void onDraw(Canvas canvas) {
//This saves off the matrix that the canvas applies to draws, so it can be restored later.
canvas.save();
//now we change the matrix
//We need to rotate around the center of our text
//Otherwise it rotates around the origin, and that's bad.
float py = this.getHeight()/2.0f;
float px = this.getWidth()/2.0f;
canvas.rotate(180, px, py);
//draw the text with the matrix applied.
super.onDraw(canvas);
//restore the old matrix.
canvas.restore();
}
我还要说的是,我写了一个实现了onDraw()
方法的类,它运行得非常好。
您也可以使用此示例。它更好,因为您不需要通过 Y 轴移动项目。
canvas.save();
canvas.scale(1f, -1f, super.getWidth() * 0.5f,
super.getHeight() * 0.5f);
canvas.drawBitmap(arrow,
rect.centerX() - (arrow.getWidth() * 0.5F), rect.bottom,
null);
canvas.restore();
ImageView
和ImageButton
这样的小部件,然后简单地将内容旋转180度。 - CommonsWare