如何在具有通过 ArrayAdapter 放置的 ArrayList 的 ListView 上设置字体
How to set Typeface on ListView which has an ArrayList placed through ArrayAdapter
我想在下面的代码中将字体设置为列表视图。附上所有相关文件。
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>
非常感谢您的帮助!
您可以创建如下所示的自定义文本视图,并在自定义文本视图中设置所需的字体 class,如下所示。
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
}
并在您的 xml (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" >
<com.example.CustomTextView 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>
现在字体将应用于文本视图。
1- 在 main
目录
中创建一个目录 assets
2- 从 Internet 下载所需的字体并将文件复制到资产
3- 在您的适配器中
Typeface name_For_Typeface;
name_For_Typeface= Typeface.createFromAsset(getContext().getAssets(), "yourfont.ttf");
textview.setTypeface(name_For_Typeface);
哦,谢谢,但是你弄错了,但我改了。看看这个:
您的代码:
/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
您忘记调用 init() 所以它没有进行任何更改。您可以像 init(context,attrs)
这样在 init 中传递上下文和属性。你也可以这样做,而不是我这样做:
/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
我想在下面的代码中将字体设置为列表视图。附上所有相关文件。
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>
非常感谢您的帮助!
您可以创建如下所示的自定义文本视图,并在自定义文本视图中设置所需的字体 class,如下所示。
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
}
并在您的 xml (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" >
<com.example.CustomTextView 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>
现在字体将应用于文本视图。
1- 在 main
目录
assets
2- 从 Internet 下载所需的字体并将文件复制到资产
3- 在您的适配器中
Typeface name_For_Typeface;
name_For_Typeface= Typeface.createFromAsset(getContext().getAssets(), "yourfont.ttf");
textview.setTypeface(name_For_Typeface);
哦,谢谢,但是你弄错了,但我改了。看看这个:
您的代码:
/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
您忘记调用 init() 所以它没有进行任何更改。您可以像 init(context,attrs)
这样在 init 中传递上下文和属性。你也可以这样做,而不是我这样做:
/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}