Android中部分加粗字符串

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
2个回答

8

就像这样:

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

我不明白应该放在哪里?索引是什么意思?你能详细说明一下吗? - Jani
索引 = 您想从ArrayList中获取的字符串的索引;我编辑了我的答案。现在应该很清楚了。 - Ahmad
如果你想要帮助,请发布相关的代码,否则我不会下载你的项目。 - Ahmad
现在您不需要下载代码了,只需看上面。我已经更新了代码,请查看。 - Jani
我还在等你。 - Jani

0

将你的字符串放在strings.xml文件中,然后像这样进行更改:

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

并将此文本设置为如下

android:text="@string/hello"

1
没有使用.setText(Html.fromHtml(yourStringIdHere)),它是无法正常工作的。 - letroll

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接