代码之家  ›  专栏  ›  技术社区  ›  Parth Dhameliya

在android中,如何在不使用html格式的情况下格式化如下所示的单个textview?

  •  -4
  • Parth Dhameliya  · 技术社区  · 8 年前

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

        This is my first textview
            This is my second 
              textview this 
               is my third
                textview
    
    4 回复  |  直到 8 年前
        1
  •  0
  •   Mohamed Nagy Mostafa    8 年前

    您可以使用此函数以编程方式执行此操作

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

    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)) ;
            }
        }
    }
    

    enter image description here

        2
  •  0
  •   Bhuvanesh BS    8 年前

    没有默认的实现。此外,您无法找到执行此操作的行号。 所以你必须把句子分成多行。使用 \n 重心 文本视图 .

        3
  •  0
  •   SRB Bans    8 年前
       if you use \n then your next line will be start from      
    
       This is my first textview
       <here> 
            <not here>
    

    第一 将文本字符串分为多个部分(注意:-(n+1)第n部分应小于第n部分和deff。应为两端空间)。

    创建具有垂直方向和重心的线性布局。

    第三 在该阵列上循环。 在循环中,创建一个具有重心的新textview,并将文本设置为该视图。 并将此电视添加到linearLayout。

    就是这样。

        4
  •  0
  •   Arpit Prajapati    8 年前

    这可能有用

    活动_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>
    

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