2017-02-13 70 views
1

尝试对文本进行对齐,但我无法做到这一点,我想从左侧和右侧开始以相等的边距开始文本。怎么做?在Tetview中对齐文本

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="20dp" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:lineSpacingExtra="4dp" 
     android:layout_marginRight="3dp" 
     android:layout_marginLeft="3dp" 
     android:paddingBottom="20dp" 
     android:text="@string/about_us" 
     android:textColor="@color/black" 
     android:textSize="15sp"/> 
</LinearLayout> 
+0

您可以检查它http://stackoverflow.com/questions/1292575/android-textview-justify-text –

回答

0

所有Android首先不支持完整的理由,但如果你想证明你的文本尝试这种方式

只需添加到您的build.gradle

dependencies { 
    compile 'com.github.bluejamesbond:textjustify-android:2.1.6' 
} 

https://github.com/bluejamesbond/TextJustify-Android

OR

尝试这种方式

public class MainActivity extends Activity { 

    private JustifiedTextView mJTv; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mJTv=(JustifiedTextView) findViewById(R.id.activity_main_jtv_text); 
     mJTv.setText(getResources().getString(R.string.test)); 
     mJTv.setTextSize(TypedValue.COMPLEX_UNIT_SP,20); 
     mJTv.setLineSpacing(15); 
     mJTv.setBackgroundColor(Color.RED); 
     mJTv.setAlignment(Align.LEFT); 
     mJTv.setTypeFace(Typeface.createFromAsset(getAssets(), "fonts/naskh_bold.ttf")); 

    } 
} 

XML

<ir.noghteh.JustifiedTextView 
     android:id="@+id/activity_main_jtv_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     android:padding="25dp" 
     xmlns:noghteh="http://noghteh.ir" 
     noghteh:text="@string/hello_world" 
     noghteh:textColor="@color/text" 
     noghteh:textSize="18sp" 
     > 
    </ir.noghteh.JustifiedTextView> 

https://github.com/navabi/JustifiedTextView

+0

JustifiedTextView无法解决此 –

+0

使用它oncreateview –

+0

JustifiedTextView不能解决这个符号?我添加这个依赖太编译'com.github.bluejamesbond:textjustify-android:2.1.6' –

0

对齐在TextView的机器人文本。

 

String dis="You can write your content here"; 

String text_string="<html><body style=\"text-align:justify\">"+dis+"<body><html>"; 

TextView text=setText(fromHtml(text_string));