英文:
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 && 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);
}
<!-- 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 = "This is test line 1";
String[] strArray = str.split(" ");
StringBuilder builder = new StringBuilder();
for (String s : strArray) {
builder.append(s + " ");
}
String finalString = builder.toString().trim();
Hope this helps. Feel free to ask for clarifications...
专注分享java语言的经验与见解,让所有开发者获益!
评论