android - android中的部分粗体字符串

标签 android

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

 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);                

         }
    }

TextView 的 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

最佳答案

像这样:

TextView txt = (TextView) findViewById(R.id.myTxtV);
txt.setText(Html.fromHtml(products.get(index)));

关于android - android中的部分粗体字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14175481/

相关文章:

android - 如何在Android中对HashMap(ArrayList内部)进行排序?

java - 如何在 Android Studio 1 中创建签名的 apk 时禁用像 Eclipse 这样的错误

java - 当用户触摸 Canvas 时如何执行某些操作?协助检查边界

javascript - phonegap openDatabase 无方法

android - hdpi、ldpi 还是 mdpi?

android - 如何同步 BLE Central (Mobile) 和 Peripheral 之间的通信?

Android-如何在 python 应用程序中附加 SQLite 数据库

android - 开始构建时出错 - App Center 上的 Android 构建失败

android - 如何在插入sdcard时自动安装apk?

android - 如何绘制用分隔线绘制的圆形边框描边?