3

我想加粗每行的第一个单词。但不是粗体,而是显示整个代码!

 private void addAsimA(ArrayList<String> products) {
         products.add("<b>Apple</b> It is red");
         products.add("<b>Banana</b> It is yellow");
         products.add("<b>Mango</b> It is green");

我怎样才能得到这样的输出:

苹果它是红色的

香蕉是黄色的

芒果是绿色的

这是完整的java文件

 public class MainActivity extends Activity {

    TextView txtv, txt1;
    ImageView imgv;


       // List view
    private ListView lv;

    // Listview Adapter
    ArrayAdapter<String> adapter;

    // ArrayList for Listview
    ArrayList<HashMap<String, String>> productList;

     ArrayList<String> products = new ArrayList<String>();


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         addAsimA(products);
         addAsimB(products);
    }



 private void addAsimA(ArrayList<String> products) {        

             products.add("<b>Apple</b> It is red");
             products.add("<b>Banana</b> It is yellow");
             products.add("<b>Mango</b> It is green");
              }

             private void addAsimB(ArrayList<String> products) {

                 products.add("<b>Flower</b> It is nice");
                 products.add("<b>Fog</b> It is white");
                 products.add("<b>Rose</b> It is pink");

                  lv = (ListView) findViewById(R.id.list_view);


                  // your code is added here but what is the problem?
                    TextView txt1 = (TextView) findViewById(R.id.product_list);
                    txt1.setText(Html.fromHtml(products.get(productList)));


                    // Adding items to listview
                    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_list,   products);
                    lv.setAdapter(adapter);                

         }
    }

文本视图的 XML 文件在这里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <!-- Product Name -->

     <com.examdple.customproduct.CustomTextView

            android:textColor="?android:textColorPrimary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="4sp"
            android:id="@+id/product_list"
            android:paddingLeft="20sp"
            android:paddingBottom="2sp"
            android:textSize="15sp" />

</LinearLayout>

如果有人想要该文件:http ://www.mediafire.com/?op37c1itodd8724

4

2 回答 2

8

像这样:

TextView txt = (TextView) findViewById(R.id.myTxtV);
txt.setText(Html.fromHtml(products.get(index)));
于 2013-01-05T19:29:36.313 回答
0

只需将您的字符串放入 strings.xml 并像这样更改它,

<string name="hello"><b>Hello</b> World, fh!</string>

并像这样设置这个文本

android:text="@string/hello"
于 2013-01-05T19:45:43.730 回答