如何在TextView中创建分隔符?

时间:2014-12-11 22:09:55

标签: android xml textview

a busy cat

所以我希望在位置文本之后放置分隔符,这样我的文本将更加清晰可见。我也想把Name放在Bold中,所以如果有人能帮助我,我会很感激。这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.baseball_activity);
        Intent myIntent = getIntent();
        gfR = (GroupedFeed) myIntent.getSerializableExtra(ACCOUNT_AUTHENTICATOR_RESPONSE_KEY);


        TextView textView = (TextView) findViewById(R.id.textView1);
        String htmlEncodedText = "<HR WIDTH=75 COLOR=#FF0000 SIZE=4>";
        textView.setText(Html.fromHtml(htmlEncodedText));
        textView.setMovementMethod(new ScrollingMovementMethod());

        StringBuffer buffer = new StringBuffer();
        String NEW_LINE = "\n";
        //String LINE = "<br>";
        for(RssMsg rssMsg : gfR.getFeeds()) {
            buffer.append(NEW_LINE);
            buffer.append(htmlEncodedText);
            buffer.append(" Name : <HR WIDTH=75 COLOR=#FF0000 SIZE=4>" +rssMsg.getName() + "</HR WIDTH=75 COLOR=#FF0000 SIZE=4>");
            buffer.append(NEW_LINE);
            buffer.append("   Date : " +rssMsg.getDate());
            buffer.append(NEW_LINE);
            buffer.append("   Time : " +rssMsg.getTime());
            buffer.append(NEW_LINE);
            if(rssMsg.getEventResult() == null){
                buffer.append("   Event Result : Unavailable");
            }else{
            buffer.append("   Event Result : " +rssMsg.getEventResult());
            }
            buffer.append(NEW_LINE);
            buffer.append("   Date Revised : " +rssMsg.getDateRevised());
            buffer.append(NEW_LINE);
            buffer.append("   Event Type : " +rssMsg.getEventType());
            buffer.append(NEW_LINE);
            buffer.append("   Location : " +rssMsg.getLocation());
            buffer.append(NEW_LINE);
            buffer.append(NEW_LINE);
        }
        textView.setTextColor(Color.parseColor("#FFDD00"));
        textView.setBackgroundColor(Color.parseColor("#003767"));
        textView.setText(buffer.toString());
    }      

和XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@string/title_activity_baseball" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.athletic_project.java.Baseball">


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true"
    android:maxLines="80" 
    android:scrollbars="vertical" 
    android:layout_marginTop="10dp" 
    android:text="@string/title_activity_baseball" 
    android:textSize="15sp"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

最好的方法是使用HTML格式化文本,然后以这种方式将其传递给TextView:

TextView t = ...
String myHtmlText = ...

t.setText( Html.fromHtml(myHtmlText) );

根据经验,我发现html编码是更复杂的文本格式化的最佳方法。

首先,在Activity的onCreate方法中,您需要将XML定义的TextView分配给Java对象。它是这样完成的:

public void onCreate(){

    setContentView(R.layout.[name of the XML you pasted above]);

    TextView t = (TextView) findViewById(R.id.textView1);

    String htmlEncodedText = ... //Define the text here, with a method, resources call or whatever you like

    t.setText(Html.fromHtml(htmlEncodedText));

}

或者,根据您提供的代码:

    TextView textView = (TextView) findViewById(R.id.textView1);
    textView.setMovementMethod(new ScrollingMovementMethod());

    StringBuffer buffer = new StringBuffer();
    String NEW_LINE = "<br>";
    for(RssMsg rssMsg : gfR.getFeeds()) {
        buffer.append("Name : <b>" +rssMsg.getName() + "</b>");
        buffer.append(NEW_LINE);
        buffer.append("   Date : " +rssMsg.getDate());
        buffer.append(NEW_LINE);
        buffer.append("   Time : " +rssMsg.getTime());
        buffer.append(NEW_LINE);
        if(rssMsg.getEventResult() == null){
            buffer.append("   EventResult : Unavailable");
        }else{
        buffer.append("   EventResult : " +rssMsg.getEventResult());
        }
        buffer.append(NEW_LINE);
        buffer.append("   DateRevised : " +rssMsg.getDateRevised());
        buffer.append(NEW_LINE);
        buffer.append("   EventType : " +rssMsg.getEventType());
        buffer.append(NEW_LINE);
        buffer.append("   Location : " +rssMsg.getLocation());
        buffer.append(NEW_LINE);
        buffer.append(NEW_LINE);
    }
    textView.setTextColor(Color.parseColor("#FFDD00"));
    textView.setBackgroundColor(Color.parseColor("#003767"));
    textView.setText(Html.fromHtml(buffer.toString()));
}