Android TextView 带背景的文字垂直方向显示(ems属性)

工具:Android Studio

在项目中有个这个需求:要求文字带背景,且文字是垂直方向显示的,竖着排,如图1。


图1 

但是当我满怀欣喜地写入了这段代码之后:

<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"/>

效果却是这样的:


图2 

为什么。。。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,好紧张,会不会被郭神看到,会不会被鸿洋潜,会不会被夏神打。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容