开发者

Textview placing each character on newline

I want to place each character on new line. I am using following xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="12dip"
    android:layout_marginLeft="12dip"
    android:textSize="12dip" 
    android:layout_height="fill_parent" 
    android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ"/>
</LinearLayout>

but I am getting outp开发者_C百科ut as follow(On some line 2 character are coming)

Textview placing each character on newline

.

I know that I can use \n but anybody have better option.


Here's kind of a hack that'll work:

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:ems="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="monospace"
    android:gravity="center_horizontal"
    android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
    android:textColor="@android:color/white"
    />

This relies on the monospace typeface, where all characters have the same width.


The reason your getting 2 chars on each line is due to the static width of the view. I really wouldn't approach this problem with a single view with a static width.

There are a lot of solutions to this, but based on the info you gave, I would probably create a single textview layout like you've done and dynamically inflate an array of textviews via code. AKA each char in your string gets assigned to it's own textview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜