如何在不使用 \n 或 html 格式的情况下在 android 中格式化单个文本视图?

How to format single textview like below in android without using \n or html format?

我需要这种格式 我不想使用 \n 或 br 因为我的字符串是动态的,我想修复这种格式的任何文本

    This is my first textview
        This is my second 
          textview this 
           is my third
            textview

对此没有默认实现。此外,您找不到执行此操作的行号。 所以你必须把句子分成 multi lines.Use \n 作为下一行。将重心设置为您的textView

   if you use \n then your next line will be start from      

   This is my first textview
   <here> 
        <not here>

所以,基本上你需要多个 TextView。

首先将您的文本字符串分成多个部分(注意:- (n+1)th part should be less than nth part and deff.should be both end space).

其次 创建一个具有垂直方向和重心的LinearLayout。

Third 在该数组上循环。 并在循环中创建一个具有重心的新文本视图,并将文本设置为它。 并将此电视添加到 linearLayout。

就是这样。

我试着给你想要的结果

这可能有效

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context="com.ap.mytestingapp.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/strTV"
        android:text="hello world!"
        android:gravity="center" />

</RelativeLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView tv;

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

        tv = (TextView) findViewById(R.id.strTV);

        //pass string whatever you want to show
        String apStr = printString("This is my first textview This is my second textview this is my third textview");

        //you need to define text size according to your requirement
        // I took here 25

        tv.setTextSize(25);
        tv.setText(apStr);
    }

    private String printString(String responseString) {

        String str = responseString;
        String resultStr = "";

        //you need to define cutLength Value according to your textView's textSize
        // I took it 35 when textView's textSize is 25

        int cutLength = 35;
        int count = 0;
        int from = 0;

        for(int i = 0 ; i < str.length(); i++){

            //increment of count
            count++;

            //check count value with cutLength so that we can add \n to string
            if(count == cutLength){

                // adding \n to substring
                resultStr = resultStr + str.substring(from, i) + "\n";

                // assigning from = i
                from = i;

                // reduce cutLength value
                cutLength = cutLength-10;

                // assigning count = 0
                count = 0;

            } else if(i == str.length()-1){

                // adding \n to substring
                resultStr = resultStr + str.substring(from) + "\n";
            }

        }

        //return resulting string
        return resultStr;
    }
}

您可以使用此函数以编程方式完成此操作

val text = "This is my first text view this is my second textview this is my third textview"
textView.text = proxyWifi.textFormation(text)

copy/paste 此代码用于您的项目:)

public String textFormation(String text){
    String result = "";
    String[] sentenceWords = text.split(" ");
    List<String> newSentenceWords = new ArrayList<>();
    textRec(sentenceWords, newSentenceWords, sentenceWords.length -1, 0, "");

    int spacing = 0;
    for(int i = newSentenceWords.size() -1 ; i >= 0 ; i--){
        if(i == newSentenceWords.size() -1)
            result = newSentenceWords.get(i);
        else{
            result += "\n";
            spacing += (newSentenceWords.get(i + 1).length() - newSentenceWords.get(i).length())/2;

            for(int j = 0 ; j < spacing ; j++){
                result += " ";
            }

            result += newSentenceWords.get(i);
        }
    }

    return result;

}

public void textRec(String[] words, List<String> newWords, int indexWords, int indexNewWords, String sentence){
    Log.e("sentence", sentence);
    if(indexWords >= 0){
        if(indexNewWords == 0) {
            newWords.add(words[indexWords]);
            textRec(words, newWords, indexWords - 1, ++indexNewWords, "");
        }else{
            if(newWords.get(indexNewWords - 1).length() >= sentence.length())
                if(sentence.isEmpty())
                    textRec(words, newWords, indexWords - 1, indexNewWords, words[indexWords]);
                else
                    textRec(words, newWords, indexWords - 1, indexNewWords,  words[indexWords] + " " + sentence);
            else {
                newWords.add(sentence);
                textRec(words, newWords, indexWords , ++indexNewWords, "");
            }
        }
    }else{
        if(sentence.isEmpty()){
            return;
        }else{
            newWords.set(indexNewWords - 1 ,sentence + " " + newWords.get(indexNewWords - 1)) ;
        }
    }
}

输出