我想在下面的代码中将字体设置为listview。所有相关的文件都附上。如何在通过ArrayAdapter放置ArrayList的ListView上设置字体
SearchClient.java
Toolbar mToolbar;
EditText search;
ListView listView;
ArrayAdapter<String> adapter;
ArrayList<HashMap<String, String>> productList;
Typeface typeface;
DatabaseHelper databaseHelper;
//Clients List
List<String> client_name = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_client);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mToolbar = (Toolbar) findViewById(R.id.toolbar_search_client);
setSupportActionBar(mToolbar);
typeface = Typeface.createFromAsset(getAssets(),"fonts/ProductSans-Regular.ttf");
init();
search.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
search.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ffffff")));
}
});
databaseHelper = new DatabaseHelper(this);
Cursor res = databaseHelper.TABLE_CLIENTS_view();
while (res.moveToNext()) {
client_name.add(res.getString(1));
}
adapter = new ArrayAdapter<String>(this, R.layout.search_list_item, R.id.search_client_name, client_name);
listView.setAdapter(adapter);
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchClient.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
public void init(){
search = (EditText) findViewById(R.id.search_client);
search.setTypeface(typeface);
listView = (ListView) findViewById(R.id.listView_search_client);
}
activity_search_client.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:focusableInTouchMode="true"
android:focusable="true"
android:id="@+id/rel_lay_client_search"
tools:context="com.example.monilandharia.invoice.SearchClient">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/toolbar_search_client"
layout="@layout/toolbar_search" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="@+id/listView_search_client" />
</LinearLayout>
</RelativeLayout>
search_list_item.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" >
<TextView android:id="@+id/search_client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
并感谢很多提前的帮助!
我想你需要一个自定义的ArrayAdapter。 – yennsarah
请参阅http://androidtrainningcenter.blogspot.in/2013/07/applying-custom-font-in-entire-android.html。 –
你不能设置列表视图类型的脸。在你的适配器上设置类型面。 –