如何在回收器视图中更改字体?

c3frrgcw  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(374)

我有一个按钮类型的列表 TextView 就像在水平方向上 RecyclerView 我试图改变这些按钮文本的字体,但它不起作用。我改变了字体 XML 我的项目的exo文件,并试图使用许多提示,而不是查看 TextView 但是我不能改变字体。在使用时自定义视图有什么限制吗 RecyclerView 或者我做错了?
p、 我不能改变这个房间的填充物 TextView 任何一个。
我的 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&amp;D"
        android:textAllCaps="false"
        android:textColor="#000000"
        android:textSize="13sp" />

    </LinearLayout>

我的 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_" />

我的适配器:

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);
        }
    }
}

活动中回收器视图的实现:

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");
    }
}
xfb7svmp

xfb7svmp1#

可以在viewholder构造函数中使用 Typeface.createFromAsset ```
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"));
}

}

相关问题