[英]How to change the font in recycler view?
I have a list of Button type TextView
s like set in a horizontal RecyclerView
and I'm trying to change the font of the text of these buttons and it's not working.我有一个按钮类型
TextView
的列表,就像在水平RecyclerView
中设置的一样,我正在尝试更改这些按钮的文本字体,但它不起作用。 I've changed the font in the XML
file of my item to exo, and tried to use many tips of view instead of the TextView
but i can't change the font anyway.我已将我的项目的
XML
文件中的字体更改为 exo,并尝试使用许多视图提示而不是TextView
,但无论如何我都无法更改字体。 is there any limitation about customization of the view when using RecyclerView
or I'm doing it wrong?使用
RecyclerView
时自定义视图是否有任何限制,或者我做错了?
ps I can't change the padding of the TextView
either. ps 我也无法更改
TextView
的填充。
My RecyclerView
item:我的
RecyclerView
项目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="32dp">
<TextView
android:id="@+id/rpgListBtn"
android:layout_width="130dp"
android:layout_height="32dp"
android:layout_weight="1"
android:background="@drawable/ic_rpg_list"
android:fontFamily="@font/exo_teste"
android:gravity="center"
android:includeFontPadding="true"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="D&D"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="13sp" />
</LinearLayout>
My RecyclerView
:我的
RecyclerView
:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rpgListView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:clipToPadding="false"
android:paddingLeft="22dp"
android:paddingRight="8dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/escolha_um_" />
my adapter:我的适配器:
package com.example.rpglink.Adapters;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.rpglink.R;
import java.util.List;
public class RpgListAdapter extends RecyclerView.Adapter<RpgListAdapter.MyViewHolder> {
private List<String> titulos;
public RpgListAdapter(List<String> titulos) {
this.titulos = titulos;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemLista = LayoutInflater.from(parent.getContext()).inflate(R.layout.rps_list_view_holder,parent,false);
return new MyViewHolder(itemLista);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
String titulo = titulos.get(position);
holder.titulo.setText(titulo);
}
@Override
public int getItemCount() {
return titulos.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView titulo;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.rpgListBtn);
}
}
}
the implementation of recycler view in the activity: Activity中recycler view的实现:
package com.example.rpglink.Activitys;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.rpglink.Adapters.RpgListAdapter;
import com.example.rpglink.Helpers.HorizontalSpaceItemDecoration;
import com.example.rpglink.R;
import java.util.ArrayList;
import java.util.List;
public class HomeC extends AppCompatActivity {
private RecyclerView recyclerView;
private RpgListAdapter adapter;
private List<String> lista = new ArrayList<>();
public HomeC() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_c);
criarTitulos();
recyclerView = findViewById(R.id.rpgListView);
recyclerView.setHasFixedSize(false);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
adapter = new RpgListAdapter(lista);
recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new HorizontalSpaceItemDecoration(32));
}
public void criarTitulos(){
this.lista.add("D&D");
this.lista.add("CyberPunk");
this.lista.add("Call of Cthulhu");
this.lista.add("Tormenta");
}
}
You can set it in the ViewHolder constructor using Typeface.createFromAsset
您可以使用
Typeface.createFromAsset
在 ViewHolder 构造函数中设置它
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView titulo;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
titulo = itemView.findViewById(R.id.rpgListBtn);
titulo.setTypeface(Typeface.createFromAsset(itemView.getContext().getAssets(),
"font/exo_teste"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.