zoukankan      html  css  js  c++  java
  • 实现TextView 文字排版,分散两端对齐

    參考:http://www.cnblogs.com/lcyty/p/3265335.html


    方法一:使用HTML

    TextView textview=(TextView)findViewbyId(R.id.text);
    textview.setText(Html.fromHtml(page.getContent()));

    方法二:用代码实现


    直接上代码:


    import android.app.Activity;
    import android.database.Cursor;
    import android.graphics.Point;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.Display;
    import android.view.Gravity;
    import android.view.Menu;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
        static Point size;
        static float density;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Display display = getWindowManager().getDefaultDisplay();
            size=new Point();
            DisplayMetrics dm=new DisplayMetrics();
            display.getMetrics(dm);
            density=dm.density;
            display.getSize(size);
    
    
            TextView tv=(TextView)findViewById(R.id.textView1);
            Typeface typeface=Typeface.createFromAsset(this.getAssets(), "Roboto-Medium.ttf");
            tv.setTypeface(typeface);
            tv.setLineSpacing(0f, 1.2f);
            tv.setTextSize(10*MainActivity.density);
    
            //some random long text
             String myText=getResources().getString(R.string.my_text);
    
             tv.setText(myText);
            TextJustification.justify(tv,size.x);
    
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    import java.util.ArrayList;
    
    import android.graphics.Paint;
    import android.text.TextUtils;
    import android.widget.TextView;
    
    public class TextJustification {
    
        public static void justify(TextView textView,float contentWidth) {
            String text=textView.getText().toString();
            Paint paint=textView.getPaint();
    
            ArrayList<String> lineList=lineBreak(text,paint,contentWidth);
    
            textView.setText(TextUtils.join(" ", lineList).replaceFirst("\s", ""));
        }
    
    
        private static ArrayList<String> lineBreak(String text,Paint paint,float contentWidth){
            String [] wordArray=text.split("\s"); 
            ArrayList<String> lineList = new ArrayList<String>();
            String myText="";
    
            for(String word:wordArray){
                if(paint.measureText(myText+" "+word)<=contentWidth)
                    myText=myText+" "+word;
                else{
                    int totalSpacesToInsert=(int)((contentWidth-paint.measureText(myText))/paint.measureText(" "));
                    lineList.add(justifyLine(myText,totalSpacesToInsert));
                    myText=word;
                }
            }
            lineList.add(myText);
            return lineList;
        }
    
        private static String justifyLine(String text,int totalSpacesToInsert){
            String[] wordArray=text.split("\s");
            String toAppend=" ";
    
            while((totalSpacesToInsert)>=(wordArray.length-1)){
                toAppend=toAppend+" ";
                totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1);
            }
            int i=0;
            String justifiedText="";
            for(String word:wordArray){
                if(i<totalSpacesToInsert)
                    justifiedText=justifiedText+word+" "+toAppend;
    
                else                
                    justifiedText=justifiedText+word+toAppend;
    
                i++;
            }
    
            return justifiedText;
        }
    
    }
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    
        tools:context=".MainActivity" 
        >
    
    
    
        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
    
                 >
                <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
            </LinearLayout>
        </ScrollView>
    
    </RelativeLayout>


  • 相关阅读:
    代码开发,测试及发布
    需求改进&系统设计
    综合系统开发----需求分析
    自我介绍+课后6问
    动态规划: 最大m子段和问题的详细解题思路(JAVA实现)
    mybatis typealias 问题
    对偶微分形式
    ASP.NET Web API 直到我膝盖中了一箭【1】基础篇
    第一篇博客,写点什么好呢?
    docker之MySQL主从复制
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/3946786.html
Copyright © 2011-2022 走看看