[英]How do I insert a line break in a String to show on an Android TextView widget?
[英]how can I, character and words count from string to make a line break insert in android
我如何从字符串中计数字符和单词以在Android中插入换行符
我有最多150个字母的单词文本输出,并希望所有50个字母都带有换行符而不分隔单词。 我该如何实施? 这是一个代码片段....
ll = (TableLayout) rootview.findViewById(R.id.displayLinear);
TableRow row = new TableRow(getActivity());
consoleLocalStore.storeConsoleData(consolex);
LayoutInflater inflater = getLayoutInflater(getArguments());
View theInflatedView = inflater.inflate(R.layout.activity_consolex,
null);
TextView attid = (TextView) theInflatedView.findViewById(R.id.output);
attid.setText(String.valueOf(consolex.output));
attid = new TextView(getActivity());
row.addView(attid);
row.setGravity(Gravity.CENTER_HORIZONTAL);
row.addView(theInflatedView);
ll.addView(row, i);
假设您想在索引中插入一个\\n
,使其更接近50。您可以使用StringBuilder:
StringBuilder builder = new StringBuilder(yourString);
boolean inserted;
int index = 50;
do{
if(yourString.charAt(index)==' '){
builder.insert(index,'\n');
inserted=true;
}
index++;
/* or index-- if you want to insert \n before 50th character*/
}while(inserted == false);
希望这对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.