Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...