android中的全文理由

时间:2011-12-27 12:31:01

标签: android

我正在从服务器浏览数据并通过page.getContent(),我的textview有默认的引力(左),所以文本左对齐,我也想要正确对齐(因为我们在MS Office中证明文本)。 ......有没有办法证明文本的合理性。

我把android:garavity =“left | right”它也不起作用。

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

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题并没有找到任何好的解决方案,所以我写了自己的课,就是这样。 只需要调用带有两个参数的静态对齐函数,该类将为您完成剩下的工作:)希望这会有所帮助。

  1. 文字视图对象
  2. 内容宽度(文字视图的总宽度)
  3. // MainActivity

    package com.fawad.textjustification;
    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;
        }
    
    }
    

    // TextJustificationClass

    package com.fawad.textjustification;
    
    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;
        }
    
    }
    

    // XML

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

答案 1 :(得分:0)

AFAIK,完全合理,即文本向左和向右冲洗,在API 14上不可用于Android 14.我不知道将来是否会包含此支持。您可能希望在TextView的布局中尝试以下属性。有些人声称它对他们有用(虽然它对我不起作用):

android:gravity="fill_horizontal"