gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:10:05 25 4
gpt4 key购买 nike

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

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com