我想加粗每行的第一个单词。但不是粗体,而是显示整个代码!
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>