有没有办法在文本视图中更改单词之间的空格间距?

huangapple 未分类评论44阅读模式
英文:

is there any way change the spaces between words of text in text view

问题

    public void fullGhazal(Integer jk) {
        ghazalLayout.removeAllViews();

        Cursor data = dataBaseHelper.getghazalut(jk);
        if (data.getCount() != 0 && data.moveToFirst()) {

            String title = data.getString(0);
            str1 = data.getString(1);
            Spannable spannable = Spannable.Factory.getInstance().newSpannable(str1);
            String str2 = spannable.toString();

            //checking split text option

            String[] replace = str1.split("\n\n");

            int length = replace.length;

            expandableTextViews = new com.urdushairiclassics.shairykeustad.Utils.ExpandableTextView[length];

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
            );

            for (kh = 0; kh < length; kh++) {

                LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
                String huzefa = replace[kh];

                Spannable spannable1 = Spannable.Factory.getInstance().newSpannable(huzefa);

                expandableTextViews[kh] = (com.urdushairiclassics.shairykeustad.Utils.ExpandableTextView) View.inflate(this, R.layout.expandabletextviews, null);
                View viewDivider = new View(this);
                int dividerHeight = (int) (getResources().getDisplayMetrics().density * 1); // 1dp to pixels
                viewDivider.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        dividerHeight
                ));
                viewDivider.setBackgroundColor(Color.BLACK);

                expandableTextViews[kh].setText((CharSequence) spannable1);
                expandableTextViews[kh].setTag(sherCount);
                expandableTextViews[kh].setId(kh);
                expandableTextViews[kh].setTextSize(2, this.textSize);

                expandableTextViews[kh].setTypeface(AppUtils.getTypeFace(getAssets()));
                expandableTextViews[kh].setLayoutParams(params);
                ghazalLayout.addView(expandableTextViews[kh]);
                ghazalLayout.addView(viewDivider);
            }

"Text displayed in my app:"

有没有办法在文本视图中更改单词之间的空格间距?

"How I want to display it:"

有没有办法在文本视图中更改单词之间的空格间距?

I am using custom TextView to show the data fetched from the database. The displayed text is not aligned properly. I have attached images to help you better understand the problem.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

public void fullGhazal(Integer jk) {
        ghazalLayout.removeAllViews();

        Cursor data = dataBaseHelper.getghazalut(jk);
        if (data.getCount() != 0 &amp;&amp; data.moveToFirst()) {


            String title = data.getString(0);
            str1 = data.getString(1);
            Spannable spannable = Spannable.Factory.getInstance().newSpannable(str1);
            String str2 = spannable.toString();

            //checking split text option

            String[] replace = str1.split(&quot;\n\n&quot;);

            int length = replace.length;


            expandableTextViews = new com.urdushairiclassics.shairykeustad.Utils.ExpandableTextView[length];


            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT
            );


            for (kh = 0; kh &lt; length; kh++) {

                LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
                String huzefa = replace[kh];

                Spannable spannable1 = Spannable.Factory.getInstance().newSpannable(huzefa);

                expandableTextViews[kh] = (com.urdushairiclassics.shairykeustad.Utils.ExpandableTextView) View.inflate(this, R.layout.expandabletextviews, null);
                View viewDivider = new View(this);
                int dividerHeight = (int) (getResources().getDisplayMetrics().density * 1); // 1dp to pixels
                viewDivider.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        dividerHeight
                ));
                viewDivider.setBackgroundColor(Color.BLACK);


                expandableTextViews[kh].setText((CharSequence) spannable1);
                expandableTextViews[kh].setTag(sherCount);
                expandableTextViews[kh].setId(kh);
                expandableTextViews[kh].setTextSize(2, this.textSize);

                expandableTextViews[kh].setTypeface(AppUtils.getTypeFace(getAssets()));
                expandableTextViews[kh].setLayoutParams(params);
                ghazalLayout.addView(expandableTextViews[kh]);
                ghazalLayout.addView(viewDivider);


            }

<!-- end snippet -->

Text displayed in my app:
有没有办法在文本视图中更改单词之间的空格间距?

How I want to display it:
有没有办法在文本视图中更改单词之间的空格间距?

I am using custom TextView to show the data fetched from database. Text displayed not being aligned properly. I have attached image so that you people can better understand the problem.

答案1

得分: 0

遍历句子,在其中添加一些空格。以下是示例代码...

String str = "这是测试行1";
String[] strArray = str.split(" ");

StringBuilder builder = new StringBuilder();
for (String s : strArray) {
    builder.append(s + "   ");
}

String finalString = builder.toString().trim();

希望对您有所帮助。如有疑问,请随意提问...

英文:

Iterate through the sentence and add some spaces in between. Here's the sample code...

String str = &quot;This is test line 1&quot;;
String[] strArray = str.split(&quot; &quot;);

StringBuilder builder = new StringBuilder();
for (String s : strArray) {
     builder.append(s + &quot;   &quot;);
}

String finalString = builder.toString().trim();

Hope this helps. Feel free to ask for clarifications...

huangapple
  • 本文由 发表于 2020年7月26日 23:03:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/63101860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定