我想在 android xml 布局文件上显示一些 UI 元素。 我尝试制作一个应用程序,其中两个玩家可以坐在移动设备的两端,并互相对战。
因此需要显示一些旋转 180 度的 Button。
这可能吗?我尝试了 android:gravity,但这没有用。
谢谢你的帮助, 马丁
最佳答案
我建议你看看this thread , 其中讨论了类似的问题。尽管问题是关于 TextView 组件,但 Button 扩展了 TextView,因此将其适应按钮应该是微不足道的。该问题的 OP 最终确定了以下 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()
方法的类,并且效果很好。
关于java - 如何显示旋转 180 度左右的 Android UI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3866976/