解決安卓TextView異常換行,參差不齊等問題

來源:互聯網
上載者:User

標籤:

參考:http://blog.csdn.net/u012286242/article/details/28429267?utm_source=tuicool&utm_medium=referral

關於TextView異常換行,參差不齊問題分析:安卓TextView第二行開始(包括第二行),每行開頭第一個字元不能為字母、數字、標點符號、以及特殊字元,因為它們都為半形字元,所以我們要將半形字元改為全形字元……

       沒有轉換為半形字元前:                                                    

                                                                        

轉換為全形字元後:

                           

布局檔案:main.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"
              android:background="#ffffff"
              android:gravity="center">
    <TextView android:id="@+id/Content"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#000000"
              android:layout_marginLeft="10dp"
              android:layout_marginRight="10dp"
              android:textSize="16dp"/>
</LinearLayout>

 java檔案:MyActivity.java

package com.example.androidTest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyActivity extends Activity {
    private TextView Content;
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Content=(TextView)findViewById(R.id.Content);
        String input="移動OA上線會議計劃於2012年05月19日9點在102會議室準時召開,請各位準時參加會議!房間安靜的身份辣椒粉杜蕾斯打附加賽科技大廈防雷接地薩拉會計分錄快點撒解放路口的就是大家是否繳費的盧卡斯解放東路卡薩!";
        String cc=ToSBC(input);
        Content.setText("        "+cc);
    }
    //導致TextView異常換行的原因:安卓預設數字、字母不能為第一行以後每行的開頭字元,因為數字、字母為半形字元
    //所以我們只需要將半形字元轉換為全形字元即可,方法如下
    public static String ToSBC(String input) {
        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == ‘ ‘) {
                c[i] = ‘\u3000‘;
            } else if (c[i] < ‘\177‘) {
                c[i] = (char) (c[i] + 65248);
            }
        }
        return new String(c);
    }
}

解決安卓TextView異常換行,參差不齊等問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.