繁体   English   中英

Android 的布局问题:旋转的 TextView

[英]Layout problem with Android: rotated TextView

我正在构建一个 Android 应用程序,我必须在该应用程序上以类似表格的格式表示数据。
所以我正在使用 TableLayout。 问题是我必须像下面的原始示例一样绘制一个旋转的字符串:

替代文字

如何创建布局以显示旋转的“2011”?

扩展TextView类并覆盖onDraw方法。

@Override
protected void onDraw(Canvas canvas) {
     canvas.save();
     canvas.rotate(90, xPivot, yPivot);
     super.onDraw(canvas);
     canvas.restore();

} 

有点晚了,但也许其他人可能需要这个。

您也可以使用 xml-layouts 来做到这一点。 由于您需要“类似表格”的格式,因此我建议您根据自己的目的使用 GridLayout。

在这里你可以定义你的 TextViews 并将它们分配给你想要的行和列。 使用参数android:layout_column="..."android:layout_row="..."告诉 TextView 它应该出现在哪一列和哪一行。

从 API11 开始,您可以使用名为android:rotation="..."的 xml 属性来旋转您的 TextView。 该属性使用浮点值表示旋转 TextView 的角度。

如果您正在为 API14 及更高版本开发,则可以使用android:layout_rowspan="..."属性在 GridLayout 中生成 rowspan

例如一个小代码:

 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- column 0 start --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="0" android:text="ABC" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="1" android:layout_rowSpan="3" android:rotation="-90.0" android:layout_gravity="center_vertical" android:text="2011" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="0" android:layout_row="4" android:text="DEF" /> <!-- column 0 end --> <!-- column 1 start --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="0" android:text="XXXXX" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="1" android:text="XXXXX" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="2" android:text="XXXXX" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="3" android:text="XXXXX" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1" android:layout_row="4" android:text="XXXXX" /> <!-- column 1 end --> </GridLayout>
这是布局的小截图 给定布局示例的屏幕截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM