从网址设置文本背景颜色?

时间:2012-04-03 18:32:13

标签: android url text background

所以我有一个活动,从web服务器上的.txt导入名称列表。 但是我如何设置背景颜色?在显示应用程序名称的页面上? 因为它不使用我设置的布局?哪个是roster.xml 我是否必须对.txt文件执行某些操作?

Roster.class代码:

package com.frede.iii;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.widget.ScrollView;
import android.widget.TextView;

public class IIIRoster extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.roster);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);




//      Scrollview
    ScrollView sv = new ScrollView(this);

    /* We will show the data we read in a TextView. */ 
    TextView tv = new TextView(this); 

    /* Will be filled and displayed later. */ 
    String myString = null; 
    try { 
         /* Define the URL we want to load data from. */ 
        //http://androidtest.host.org/roster.txt
         URL myURL = new URL( 
                   "http://androidtest.host.org/roster.txt"); 
         /* Open a connection to that URL. */ 
         URLConnection ucon = myURL.openConnection(); 

         /* Define InputStreams to read 
          * from the URLConnection. */ 
         InputStream is = ucon.getInputStream(); 
         BufferedInputStream bis = new BufferedInputStream(is); 

         /* Read bytes to the Buffer until 
          * there is nothing more to read(-1). */ 
         ByteArrayBuffer baf = new ByteArrayBuffer(50); 
         int current = 0; 
         while((current = bis.read()) != -1){ 
              baf.append((byte)current); 
         } 

         /* Convert the Bytes read to a String. */ 
         myString = new String(baf.toByteArray()); 
    } catch (Exception e) { 
         /* On any Error we want to display it. */ 
         myString = e.getMessage(); 
    } 
    /* Show the String on the GUI. */ 
    tv.setText(myString); 
//        Adds  textview to scrollview   
        sv.addView(tv);

//          Sets contentview to scrollview    
        this.setContentView(sv); 
    }
}

1 个答案:

答案 0 :(得分:2)

您好,您可以简单地使用tv.setBackgroundColor(COLOR.XYX)sv.setBackgroundColor(COLOR.XYX)

同时textColor可以作为tv.setTextColor(COLOR.XYX)

应用于文本

要定义自己在运行时使用的颜色,请使用以下命令:

1.在string.xml文件中使用以下标记

<color name="mycolor">#F5DC49</color>

现在在您的代码中。

tv.setTextColor(getResources().getColor(R.color.mycolor))

2.我们也可以将RGB值设置为:

tv.setTextColor(Color.rgb(170, 00, 00));