Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
290 views
in Technique[技术] by (71.8m points)

android - Justify text in textview

In my app first I have textview to show text. Then I want to justify text but in android its not possible to justify text in textview. To justify text I am taking help from this link. I am following the answer provided by @Kondzio but its not working. I dont know whats wrong in my code.

Code-

public class Benefits extends Activity{
    private Button back;
    LinearLayout bText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.benefits);
        WebView view = new WebView(this);
        view.setVerticalScrollBarEnabled(true);
        ((LinearLayout)findViewById(R.id.bText)).addView(view);
        view.loadData(getString(R.string.benef), "text/html", "utf-8");

        back.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            finish();
        }
    });
  }
}

Xml-

<LinearLayout 
       android:id="@+id/bText"
       android:orientation="horizontal"
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:layout_weight="4"
       android:gravity="center">            
 </LinearLayout>

In strings.xml-

<string name="benef">
        <![CDATA[
        <html>
        <head></head>
        <body style="text-align:justify;color:gray;background-color:black;">
         "1.some text

          2.some text

          .............
 </body>
</html>
]]>
</string> 

I am setting scrollbarenabled to true to make vertical scrolling on my text.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

// converting all the language content to justify using html tags.

String youtContentStr = String.valueOf(Html
                .fromHtml("<![CDATA[<body style="text-align:justify;color:#222222; ">"
                            + getResources().getString(R.string.passthe_content)
                            + "</body>]]>"));

    view.loadData(youtContentStr, "text/html", "utf-8");

EDIT: or change your code with added back slash

<body style="text-align:justify;color:gray;background-color:black;">

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...