2011-01-08 121 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