Java 如何将自定义ListView字体更改为自定义字体?
以下是我的XML:Java 如何将自定义ListView字体更改为自定义字体?,java,android,listview,fonts,Java,Android,Listview,Fonts,以下是我的XML: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:background="#E0E0E0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#E0E0E0"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ccb.lldm.lldmhimnario.Cantos">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:elevation="25dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#303F9F"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:layout_width="wrap_content"
android:textColor="#E0E0E0"
android:text="Cantos"
android:id="@+id/toolbarCantos"
android:textSize="25sp"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ListView"
android:layout_below="@+id/toolbar"
android:layout_alignParentBottom="true">
</ListView>
</RelativeLayout>
}
这是我的最后一个Java文件:
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
private static LayoutInflater inflater = null;
public CustomAdapter(Cantos cantos, String[] cantos1) {
result = cantos1;
context = cantos;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return result.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
return rowView;
}
}
我刚接触安卓系统,我已经掌握了它的窍门,但在这方面我需要帮助。提前谢谢 如果要为ListView项自定义字体,应尝试在getView()中将字体设置为CustomAdapter.java类; 请看下面的代码:
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myTypeface);
return rowView;
}
您始终可以在适配器的getView()中更改字体,但如果要在代码的不同部分多次使用该字体,则始终可以扩展TextView,为此,您可以参考:
这样,每当您需要使用其他字体时,您都可以使用该字体,而不是布局文件中的常规文本视图。您是否应该为列表视图中的视图R.id.textconto设置字体谢谢!您是否知道如何使某些ListView项目将您发送到某个活动?谢谢setOnItemClickListener到ListView
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){@Override public void onItemClick(AdapterView父级,视图视图,int位置,长id){Toast.makeText(ListActivity.this,“+position,Toast.LENGTH_SHORT.show();})代码>先生,谢谢您的回复,但我不是在找这个。我想知道如何编码,这样当我按下listview中的随机项时,它会将我发送到另一个活动。你知道怎么做吗?是的,但是你需要为这11个项目创建11个类活动!您是否知道如何使某些ListView项目将您发送到某个活动?谢谢是的,当然,只要在onItemClick上开始一个新的活动,如果你需要不同位置的不同活动,只需检查位置并相应地开始。检查如果答案对你有帮助,你也应该接受它和/或投票支持它。
public class CustomAdapter extends BaseAdapter {
String[] result;
Context context;
private static LayoutInflater inflater = null;
public CustomAdapter(Cantos cantos, String[] cantos1) {
result = cantos1;
context = cantos;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return result.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView tv;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
return rowView;
}
}
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.cantos_list, null);
holder.tv = (TextView) rowView.findViewById(R.id.textCanto);
holder.tv.setText(result[position]);
Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/AftaSerifThin-Regular.otf");
holder.tv.setTypeface(myTypeface);
return rowView;
}