2012-03-10 116 views
1

我设置以下字符串作为我txtMessage一个输入:如何在TextView中添加换行符和空格?

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.breaknotification); 

    TextView txtMessage = (TextView)findViewById(R.id.txtMessage); 
    String msg = "TIME RECORD  VALUE\n" + 
       "10 AM 1   20\n"+ 
       "11 AM 2   30\n"+ 
       "12 PM 3   40"; 

    txtMessage.setText(msg); 

} 

我breaknotification.xml文件是按下列内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/txtMessage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:text="" 
    android:textColor="#FFFFFF" 
    android:textSize="21sp" 
    android:textStyle="bold" /> 

</LinearLayout> 

,我想打印我的字符串按以下格式。

TIME RECORD  VALUE 
10 AM  1  20 
11 AM  2  30 
12 PM  3  40 

但它没有显示出我想要的东西;它显示我以下在我的消息,

TIME  RECORD  VALUE 
`10 AM 1  20` 
`11 AM 2  30` 
`12 PM 3  40` 

那么我怎么能实现它?

+0

你清理和生成项目?我很确定你在做什么是对的,我也会这样做。 – noob 2012-03-10 20:41:45

+0

是的,我清理,但没有工作。 – askimp 2012-03-12 07:45:11

回答

0

我发现了以下解决方案:
添加TextView的属性:机器人:字体= “等宽”

<TextView 
    android:id="@+id/txtMessage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    android:text="" 
    android:textColor="#FFFFFF" 
    android:typeface="monospace" 
    android:textSize="21sp" 
    android:textStyle="bold" /> 
3
txt_Message.setText("TIME RECORD VALUE \n10 AM 1 20 \n11 AM 2 30 \n12 PM 3 40"); 
+0

感谢您的回复,它也没有工作。 – askimp 2012-03-10 14:37:40

+0

尝试用较小的文字为例**嗨\ n在这里**并检查它是否有效,如果是这样,这意味着您的文字间距有问题 – 2012-03-10 14:41:07

+0

对不起,它也不适用于较小的文字。 – askimp 2012-03-10 14:54:40

1

这里是你回答:

txt_Message.setText("TIME RECORD VALUE \n10 AM 1 20 \n11 AM 2 30 \n12 PM 3 40"); 

您也可以在TextView中使用HTML代码Html.fromHtml:

txt_Message.setText(Html.fromHtml("Test<br>Text2")); 

这是不是最好的使用,但它会让你也有一个linebreak。

+0

感谢您的快速回复,我不想在字符串输入中使用Html标记 – askimp 2012-03-10 14:26:06

+0

尝试使用较小的文本例如**您好\ n有**并检查它是否工作,如果是这样就意味着你的文字间距有问题 – 2012-03-10 14:40:48

+0

它不工作吗?也许你的textView上有singleLine =“true”。 – ChapMic 2012-03-10 14:56:10

0
txt_Message.setText("TIME\tRECORD\tVALUE\n10 AM\t1\t20\n11 AM\t2\t30\n12 PM\t3\t40"); 

可以试试制表符?

+0

抱歉它没有工作。 – askimp 2012-03-12 07:46:42

相关问题