0

我目前正在工作的应用程序中有很多字符串数组。我在我的 ListViews 中调用那些字符串数组。我想将我的字符串数组项中的某些字符串更改为粗体。我已经使用了 Html.fromHtml() 和 "<[DATA[ ]]>" 但它什么也没发生。我怎样才能做到这一点?

字符串.java

<string-array name="procedure_items">
    <item>• State: This is an emergency </item>
    <item>• Give the dispatcher:</item>
    <item>\t    •  The nature of emergency</item>
    <item>\t    •  Your name</item>
    <item>\t    •  Phone number from which you are calling</item>
    <item>\t    •  Your location/location of the emergency</item>
</string-array>

项目.xml

<?xml version="1.0" encoding="utf-8"?>

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/lbl_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

</TextView>

mainActivity.java

private void populateListView() {
    String[] myItems = getResources().getStringArray(R.array.procedure_items);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            R.layout.items, myItems);

    // Configure the list view.
    lv_procedures.setAdapter(adapter);

调用时,输出必须是:

• 状态:“这是紧急情况

• 给调度员:

  • 紧急情况的性质
  • 你的名字
  • 您拨打的电话号码
  • 您的位置/紧急情况的位置
4

2 回答 2

12

您可以将<b> </b>标签用于粗体并使用以下命令转义双引号\

<string-array name="procedure_items">
    <item>• State: <b>\"This is an emergency\"</b></item>
    <item>• Give the dispatcher:</item>
    <item>\t    •  <b>The nature of emergency</b></item>
    <item>\t    •  <b>Your name</b></item>
    <item>\t    •  <b>Phone number from which you are calling</b></item>
    <item>\t    •  <b>Your location/location of the emergency</b></item>
</string-array>

并使用getTextArray()而不是getStringArray()后者将删除样式化的文本信息。因此,示例类如下所示:

package com.example.stringarraybolditem;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<CharSequence>(this, R.layout.items, getResources()
                .getTextArray(R.array.procedure_items)));
    }
}

结果:

字符串数组粗体项目


以下是原始ListPreference实现

ListPreference带有<b></b>标签的项目:

ListPreference 带有粗体效果的项目

以及对应ListPreference的 res/xml/bold_item_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <ListPreference
        android:entries="@array/procedure_items"
        android:entryValues="@array/procedure_items_values"
        android:title="@string/bold_item_test" />
    
</PreferenceScreen>
于 2013-10-07T02:58:54.030 回答
0

尝试使用自定义单元格并放置一个条件语句来放置包含该文本/字符串的控件所需的任何格式。

这是在 android 中使用自定义单元格的好方法:

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

于 2013-10-07T01:04:18.823 回答