2013-06-12 45 views
19

我正在嘗試創建類似以下的內容。Android中的垂直TextView

enter image description here

使用的LinearLayout和TableLayout。但是我在垂直文本上停頓了一下。在旋轉之後,它佔用的空間比它應該多。

fllowing是XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/tv_CONSEQUENCES" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:rotation="-90" 
     android:text="CONSEQUENCES" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TableLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

     </TableRow> 
     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Button" /> 

     </TableRow> 
    </TableLayout> 

</LinearLayout> 

和輸出這樣。

enter image description here

垂直文本花費比它應該需要更多的空間。也沒有出現全文。 :(

+1

看看那個帖子 http://stackoverflow.com/questions/2888780/is-it-possible-to-write-vertically- in-a-textview-in-android 祝你好運 –

回答

15

你應該從以下提到的鏈接得到的回答你的問題:

vertical text in textview

+2

有兩個接近的文章提到的鏈接指的是。第一種方法雖然需要自定義的TextView,但它並不會在旋轉的TextView的右側留下任何空白空間。 – lalitm

1

必須擴展TextView並覆蓋其onDraw

0

爲什麼不把填充?所以它會在你的screen.just中心嘗試,如果你喜歡

0

把所有的文字鑑於線性佈局,並嘗試給予適當佈局重量爲您解決問題

-1

如果要複製你的形象,我會用一個GridView或無按鈕的TableLayout,只需設置合適的聽衆,如果你不需要做任何動作。這樣你可以設定固定的高度和寬度。

13

您可以使用下面的代碼:

android:rotation="270" 
+0

這導致整個TextView包括它的背景旋轉,這可能不是可取的 –

+0

是的,所以最好的方式是自定義TextView垂直。 你可以使用這個例子我搜索谷歌:http://www.pocketmagic。net/2010/12/android-vertical-textview-custom-angle-text/ 我希望能幫到你:) – quangson91

+0

它仍然需要額外的空間 – Prabs

0
 android:rotation="-90" 
     android:layout_gravity="center" 

爲我工作。

3

的解決方案中沒有爲我工作,但我發現馬克阿利森這篇博客文章: https://blog.stylingandroid.com/verticaltext-part-1/

public class VerticalTextView extends TextView 
{ 
    final boolean topDown; 

    public VerticalTextView(Context context, 
     AttributeSet attrs) 
    { 
     super(context, attrs); 
     final int gravity = getGravity(); 
     if (Gravity.isVertical(gravity) 
      && (gravity & Gravity.VERTICAL_GRAVITY_MASK) 
      == Gravity.BOTTOM) 
     { 
      setGravity( 
       (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) 
        | Gravity.TOP); 
      topDown = false; 
     } 
     else 
     { 
      topDown = true; 
     } 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, 
     int heightMeasureSpec) 
    { 
     super.onMeasure(heightMeasureSpec, 
      widthMeasureSpec); 
     setMeasuredDimension(getMeasuredHeight(), 
      getMeasuredWidth()); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     TextPaint textPaint = getPaint(); 
     textPaint.setColor(getCurrentTextColor()); 
     textPaint.drawableState = getDrawableState(); 

     canvas.save(); 

     if (topDown) 
     { 
      canvas.translate(getWidth(), 0); 
      canvas.rotate(90); 
     } 
     else 
     { 
      canvas.translate(0, getHeight()); 
      canvas.rotate(-90); 
     } 

     canvas.translate(getCompoundPaddingLeft(), 
      getExtendedPaddingTop()); 

     getLayout().draw(canvas); 
     canvas.restore(); 
    } 
} 

旋轉由重力完成。所以一定要設置這個在你的XML:

<com.stylingandroid.verticaltext.VerticalTextView 
    style="@style/verticalTextStyle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="bottom|right" 
    android:text="@string/text" /> 
+0

將視圖看起來像改變android:gravity =「bottom | right」to android:gravity =「bottom | center」 – Goodlife