android - 在列表视图中使用粗体(仅用于每个条目的一部分)
问题描述
我有一个包含许多项目的列表视图。每个项目都有一个数字作为其第一个字符。我希望列表的每个条目的数字都是粗体的。我有一个列表数组,我将其提供给数组适配器以制作列表视图。值不稳定,因此我无法将它们放入xml
文件中。有什么方法可以实现我的目标吗?我正在制作一个“当我的公共汽车到达时”应用程序
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search_view"
android:queryHint="Αναζήτηση Γραμμής"
android:inputType="text">
</SearchView>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<ListView
android:id="@+id/listview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
android:visibility="gone"
android:paddingTop="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</android.support.v4.widget.SwipeRefreshLayout>
private void initUI() {
final ArrayList<String> arrayList = new ArrayList<>();
try {
AssetManager assetManager = getAssets();
ObjectInputStream input = new ObjectInputStream(new GZIPInputStream((assetManager.open("names.dat"))));
Object readObject = input.readObject();
input.close();
ArrayList<String> names = (ArrayList<String>) readObject;
arrayList.addAll(names);
routes = read_files("routes.dat");
line_route = read_files("line_route.dat");
lastcall= new ArrayList<String>();
editsearch= (SearchView) findViewById(R.id.search_view);
editsearch.setOnQueryTextListener(this);
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
swipeRefreshLayout.setOnRefreshListener(this);
Runnable refresh = new Runnable() {
public void run() {
if ((MainActivity.this.afikseis.getVisibility()==View.VISIBLE)&& !(swipeRefreshLayout.isRefreshing())){
get_time(lastcall.get(0),lastcall.get(1),lastcall.get(2));
}
}
};
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(refresh, 10, 30, TimeUnit.SECONDS);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
print_routes(arrayList.get(i), line_route, routes);
print_routes(arrayAdapter.getItem(i).toString(), line_route, routes);
}
});
}
提前致谢
解决方案
您需要使用 SpannableString。以下是一些有用的链接:
解释 SPAN_EXCLUSIVE_EXCLUSIVE 等 Span 标志的含义
一种可能的方法是遍历您的列表并使用链接中的方法更改每个字符串,然后在数组适配器中使用此更新的列表。
您可以创建一个函数,在每次将字符串添加到列表时执行此操作。该函数可以将粗体文本仅应用于列表视图最后一项中的第一个数字。