Android 拆分文本,使文本视图中的文本视图为false
我有一个文本文件,我想在文本视图中显示它的文本,我在运行时设置它们的文本 这是我的代码,用于我的程序:Android 拆分文本,使文本视图中的文本视图为false,android,android-layout,Android,Android Layout,我有一个文本文件,我想在文本视图中显示它的文本,我在运行时设置它们的文本 这是我的代码,用于我的程序: while(text.length() > 0) { text = text.trim(); int index = text.indexOf("L1"); int index2 = 0; temp = ""; if(index > -1 && index == 0)
while(text.length() > 0)
{
text = text.trim();
int index = text.indexOf("L1");
int index2 = 0;
temp = "";
if(index > -1 && index == 0)
{
index2 = text.indexOf("/L1");
temp = text.substring(index + 2, index2);
temp.trim();
t = temp.toCharArray();
text = text.substring(index2 + 3, text.length());
index = text.indexOf("\r\n");
while(index > -1 && index == 0)
{
temp += "\r\n";
text = text.substring(index + 2, text.length());
index = text.indexOf("\r\n");
}
temp = String.valueOf(t);
}
else
{
index = text.indexOf("L2");
if(index > -1 && index == 0)
{
index2 = text.indexOf("/L2");
temp = text.substring(index + 2, index2);
text = text.substring(index2 + 3, text.length());
index = text.indexOf("\r\n");
while(index > -1 && index == 0)
{
temp += "\r\n";
text = text.substring(index + 2, text.length());
index = text.indexOf("\r\n");
}
}
else
{
index = text.indexOf("List");
if(index > -1 && index == 0)
{
index2 = text.indexOf("/List");
temp = text.substring(index + 4, index2);
text = text.substring(index2 + 5, text.length());
index = text.indexOf("\r\n");
while(index > -1 && index == 0)
{
temp += "\r\n";
text = text.substring(index + 2, text.length());
index = text.indexOf("\r\n");
}
}
else
{
text = text.trim();
index = text.indexOf("Plain");
if(index > -1 && index == 0)
{
index2 = text.indexOf("/Plain");
temp = text.substring(index + 5, index2);
text = text.substring(index2 + 6, text.length());
index = text.indexOf("\r\n");
while(index > -1 && index == 0)
{
temp += "\r\n";
text = text.substring(index + 2, text.length());
index = text.indexOf("\r\n");
}
}
}
}
}
}
for(int i = 0; i < texts.length; i++)
{
TextView tv = new TextView(this);
tv.setText(Farsi.Convert(texts[i]));
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/naz.ttf");
tv.setTypeface(font);
tv.setTextColor(Color.BLACK);
tv.setGravity(Gravity.RIGHT);
tv.setPadding(10, 10, 10, 10);
tv.setLayoutParams(params);
linearLayout.addView(tv);
}
layout.addView(linearLayout);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
this.addContentView(layout, p);
}
while(text.length()>0)
{
text=text.trim();
int index=text.indexOf(“L1”);
int index2=0;
温度=”;
如果(索引>-1&&index==0)
{
index2=text.indexOf(“/L1”);
temp=文本子字符串(索引+2,索引x2);
温度微调();
t=温度toCharArray();
text=text.substring(index2+3,text.length());
index=text.indexOf(“\r\n”);
而(索引>-1&&index==0)
{
temp+=“\r\n”;
text=text.substring(索引+2,text.length());
index=text.indexOf(“\r\n”);
}
温度=字符串的值(t);
}
其他的
{
索引=text.indexOf(“L2”);
如果(索引>-1&&index==0)
{
index2=text.indexOf(“/L2”);
temp=文本子字符串(索引+2,索引x2);
text=text.substring(index2+3,text.length());
index=text.indexOf(“\r\n”);
而(索引>-1&&index==0)
{
temp+=“\r\n”;
text=text.substring(索引+2,text.length());
index=text.indexOf(“\r\n”);
}
}
其他的
{
索引=text.indexOf(“列表”);
如果(索引>-1&&index==0)
{
index2=text.indexOf(“/List”);
temp=文本子字符串(索引+4,索引2);
text=text.substring(index2+5,text.length());
index=text.indexOf(“\r\n”);
而(索引>-1&&index==0)
{
temp+=“\r\n”;
text=text.substring(索引+2,text.length());
index=text.indexOf(“\r\n”);
}
}
其他的
{
text=text.trim();
索引=text.indexOf(“普通”);
如果(索引>-1&&index==0)
{
index2=text.indexOf(“/Plain”);
temp=文本子字符串(索引+5,索引x2);
text=text.substring(index2+6,text.length());
index=text.indexOf(“\r\n”);
而(索引>-1&&index==0)
{
temp+=“\r\n”;
text=text.substring(索引+2,text.length());
index=text.indexOf(“\r\n”);
}
}
}
}
}
}
for(int i=0;i
出于某些原因,我在文本文件中使用了一些辅助词(如“L1”和“/L1”),在运行时将它们删除。但是,当我运行程序时,在输出中,文本视图中显示的文本不是正确的样式,我想将其向右对齐,但在输出中有一些额外的文本,它们不在输入文本文件中
在下面的两幅图中,我在输入文本文件中附加了带和不带辅助词的输出
带辅助词的输出:
不带辅助词的输出,在这种状态下,我使用“setContentView”函数设置布局的内容,并使用辅助词索引读取整个文件,而不是拆分文本。在此状态下,输入不包含辅助词
朋友们有什么问题?
首先,感谢:使用
String.split()
来拆分字符串。如果您有一个字符串a、B、C,并且使用
myString,它的工作原理是这样的。拆分(,)
输出将是一个子字符串数组,即[a、B、C]。但是如果您可以使用相同的分隔符,这在您的情况下很有用,即L1和L2将是相同的
第二:您可以在
layout.xml的textview
中使用android:gravity=“right”
。这将解决字符串向左对齐的问题。谢谢您的回答,但我使用以下行设置了重力:“tv.setGravity(gravity.RIGHT);”,但它对我不起作用!问题是,当我使用substring函数,并用这些文本创建新的文本视图时,输出将是post中的输出图像