天天看點

Android TextView 帶背景的文字垂直方向顯示(ems屬性)

工具:Android Studio

在項目中有個這個需求:要求文字帶背景,且文字是垂直方向顯示的,豎着排,如圖1。

Android TextView 帶背景的文字垂直方向顯示(ems屬性)

但是當我滿懷欣喜地寫入了這段代碼之後:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="1"
            android:background="@drawable/account_bg"
            android:gravity="center"
            android:text="黑貓很緊張"
            android:textColor="#c4c4c4" />
           

效果卻是這樣的:

Android TextView 帶背景的文字垂直方向顯示(ems屬性)

為什麼。。。ems不是每行顯示的個數麼?就因為我多加了個background,他就不能豎着排了。感覺心塞塞啊,不曉得這是不是Android本身的bug。是以,就繞了點:

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/account_bg">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="1"
            android:gravity="center"
            android:text="黑貓很緊張"
            android:textColor="#c4c4c4" />
    </LinearLayout>
           

在他的外面加個線性布局,gravity為center,背景加上,TextView裡面這樣寫,就可以出現圖1的效果啦。

呃,圖檔好像有點變形了,因為我的text的高度大于了背景的高度,so,做個9patch或者其他解決掉就ok了。

ps:第一次寫blog,好緊張,會不會被郭神看到,會不會被鴻洋潛,會不會被夏神打。。。