android中的全文对齐
我正在浏览服务器上的数据并浏览页面。getContent(),我的textview具有默认重力(左),因此文本是左对齐的,我也希望右对齐(正如我们在MS Office中对齐文本)…是否有任何方法对齐文本 我已经把android:garavity=“left | right”放在了一起,它也不起作用android中的全文对齐,android,Android,我正在浏览服务器上的数据并浏览页面。getContent(),我的textview具有默认重力(左),因此文本是左对齐的,我也希望右对齐(正如我们在MS Office中对齐文本)…是否有任何方法对齐文本 我已经把android:garavity=“left | right”放在了一起,它也不起作用 TextView textview=(TextView)findViewbyId(R.id.text); textview.setText(Html.fromHtml(page.getContent(
TextView textview=(TextView)findViewbyId(R.id.text);
textview.setText(Html.fromHtml(page.getContent()));
AFAIK,完全对齐,即文本向左和向右刷新,从API 14开始在Android上是不可能的。我不知道将来是否会包括这种支持。您可能希望在
文本视图的布局中尝试以下属性。有些人声称这对他们有效(尽管对我无效):
我遇到了同样的问题,并没有找到任何好的解决方案,所以我写了自己的类,在这里。
只需调用包含两个参数的静态justify函数,该类将为您完成其余工作:)希望这会有所帮助
文本视图对象
内容宽度(文本视图的总宽度)
//主要活动
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;
}
}
//文本证明类
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;
}
}
package com.fawad.textjustification;
导入java.util.ArrayList;
导入android.graphics.Paint;
导入android.text.TextUtils;
导入android.widget.TextView;
公共类文本验证{
公共静态void justify(TextView TextView,float contentWidth){
String text=textView.getText().toString();
Paint=textView.getPaint();
ArrayList lineList=换行符(文本、绘制、内容宽度);
textView.setText(TextUtils.join(“,lineList).replaceFirst(\\s“,”);
}
私有静态ArrayList lineBreak(字符串文本、绘制、浮动内容宽度){
字符串[]wordArray=text.split(“\\s”);
ArrayList lineList=新建ArrayList();
字符串myText=“”;
for(字符串字:wordArray){
if(paint.measureText(myText+“”+word)=(wordArray.length-1)){
toAppend=toAppend+“”;
totalSpacesToInsert=totalSpacesToInsert-(wordArray.length-1);
}
int i=0;
字符串justizedText=“”;
for(字符串字:wordArray){
如果(i)
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;
}
}
<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>