这种格式我需要我不想使用\ n或br,因为我的字符串是动态的,我想用这种格式修复任何文本
This is my first textview
This is my second
textview this
is my third
textview
答案 0 :(得分:0)
此处没有默认实现。此外,您无法找到执行此操作的行号。
因此,您必须将句子拆分为多行。使用\n
作为下一行。将重心设置为 textView 。
答案 1 :(得分:0)
if you use \n then your next line will be start from
This is my first textview
<here>
<not here>
所以,基本上你需要多个TextViews。
首先将您的文本字符串分配给多个部分(注意: - (n + 1)部分应小于第n个部分,deff。应该是两个结束空间)。
秒创建具有垂直方向和中心重力的LinearLayout。
该阵列上的第三次循环。 并在循环中创建一个带有重心的新文本视图,并将文本设置为它。 并将此电视添加到linearLayout。
就是这样。
答案 2 :(得分:0)
我试着按你的意愿给你结果
这可能有用
<强> 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;
}
}
答案 3 :(得分:0)
您可以使用此功能以编程方式执行此操作
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)) ;
}
}
}