TextView of Android is a text label to display text. But it can show text only horizontally by default, left to right or right to left. There are some chances that we would like to show text vertically, top to bottom or bottom to top, for layout of modern smart phone in particular. In landscape layout, the height is not enough to show all information, we have to arrange them horizontally because width is enough to hold them. But for some label, like date and time label, it would be nicer to display them in only one line, which is impossible to do in landscape. The text is one line though, it grows horizontally to push following layouts out of screen, just like this:

But the ideal layout would be like this: one line label shows itself vertical so in horizontal, it takes up only its height and leaves much free space to put other views:

Fortunately, this is totally possible in Android only with a little extra efforts. With graphics library we can draw text in any way we want: rotate in some direction, following a path and so forth. An example TextAlign in APIDemos just shows us the way to draw text following a customizedPath. We can inherite TextView and override its onDraw(), then do what ever we like in onDraw. In here, of course, to draw the text in a Path that vertically up from bottom. Here is the code:

[java] view plain copy print ?
  1. /*
  2. *fortheattributesofTextView,someworkssomenot.
  3. *1.setTextsizeworks
  4. *2.setBackgroundColorworks
  5. *3.setTextColoralsoworks
  6. *YoucanadjustthesizeofTextViewbymarginsandthedrawingareabypaddings(onlypaddingTopandpaddingBottomworks).
  7. *Forotherattributes,likelinesormaxLines,orellipsizearenotsupportedcurrently.Tosupportthem,youshouldget
  8. *theattributesvaluebeforedrawTextandapplythem.
  9. */
  10. publicclassVerticalTextViewextendsTextView{
  11. privatestaticfinalStringTAG="VerticalTextView";
  12. publicVerticalTextView(Contextcontext,AttributeSetattrs,intdefStyle){
  13. super(context,attrs,defStyle);
  14. }
  15. publicVerticalTextView(Contextcontext,AttributeSetattrs){
  16. super(context,attrs);
  17. }
  18. publicVerticalTextView(Contextcontext){
  19. super(context);
  20. }
  21. @Override
  22. protectedvoidonDraw(Canvascanvas){
  23. finalColorStateListcsl=getTextColors();
  24. finalintcolor=csl.getDefaultColor();
  25. finalintpaddingBottom=getPaddingBottom();
  26. finalintpaddingTop=getPaddingTop();
  27. finalintviewWidth=getWidth();
  28. finalintviewHeight=getHeight();
  29. finalTextPaintpaint=getPaint();
  30. paint.setColor(color);
  31. finalfloatbottom=viewWidth*9.0f/11.0f;
  32. Pathp=newPath();
  33. p.moveTo(bottom,viewHeight-paddingBottom-paddingTop);
  34. p.lineTo(bottom,paddingTop);
  35. canvas.drawTextOnPath(getText().toString(),p,0,0,paint);
  36. }
  37. }

This VerticalTextView works much like a TextView, it supports most usual attributes like color, size, background, margin and padding(only paddingTop and paddingBottom). For others, I did not implement but you can get the attributes in onDraw and apply them. Cost efforts though, it is feasible. Here is an example to use it:

[html] view plain copy print ?
  1. <com.hilton.todo.VerticalTextView
  2. android:id="@+id/header"
  3. android:layout_height="fill_parent"
  4. android:layout_width="30dip"
  5. android:textColor="#ffff00"
  6. android:textSize="24sp"
  7. android:background="#a50909"
  8. android:text="Hello,world"
  9. android:paddingBottom="40dip"/>

There might be some other ways to achieve this like rotation, which I tried first but not with success. This example really works for me and if you ever find another way please share to us.

From this example, we learn that Android is so flexible that we can achieve almost anything we want and the only cost is your efforts.

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android:Date、String、Long三种日期类型
  2. Recently studying plan
  3. 懒人爱家务_onInterceptTouchEvent与onTo
  4. android多国语言的国家代码
  5. 大型企业所需掌握技术
  6. 有关于Android多个module混淆的问题
  7. No implementation found for native Lan
  8. android adb中查看database
  9. 2018-11-13 Mac下adb以及Android(安卓)st
  10. android adb 向模拟器上传文件