2011-01-08 127 views
5

我正在建立一个Android应用程序,我必须以表格格式表示数据。 所以我使用TableLayout;问题是,我在下面的例子原画一个旋转的字符串,如:Android的布局问题:旋转TextView

alt text

如何创建我的布局,以便能够显示“2011”旋转?

在此先感谢和问候! c。

回答

7

扩展TextView类并覆盖onDraw方法。

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

} 
+0

很酷。谢谢;但是...如果我在TableRow单元格中旋转TextView,我还需要制作一种ROWSPAN,因为2011需要使用3行......我该怎么做? – Cris 2011-01-08 22:33:03

0

有点晚,但也许别人可能需要这个。

您也可以使用xml-layouts来做到这一点。正如你想要一个“表格式”的格式,我建议你为你的目的使用一个GridLayout。

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

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

的如果要为API14和上述可以使用android:layout_rowspan="..."属性用于产生行跨度在GridLayout的

例如有点代码显影:

<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>
这是一个小截图布局如何看起来像 screenshot of the given layout example