【问题标题】:how to change custom font inside listview using kotlin android如何使用 kotlin android 更改列表视图中的自定义字体
【发布时间】:2018-04-20 14:31:42
【问题描述】:

我在网上找了很多方法来解决我的问题,但没有找到好的解决方案。

目标:如何更改 ListView 中的字体?

XML 布局代码

<ListView
android:id="@+id/tvListDrinki"
android:layout_width="match_parent"
android:layout_height="444dp"
android:background="@color/googleWhite"
android:layout_marginTop="196dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.518"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />

列表视图“票”代码

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_lista_drinkow"
android:orientation="horizontal">

<ImageView
    android:layout_marginLeft="20dp"
    android:id="@+id/ivDrinkImage"
    android:layout_width="50pt"
    android:layout_height="50pt"
    android:layout_weight="1"
    android:padding="10dp"
    app:srcCompat="@drawable/martini" />

<LinearLayout
    android:layout_width="90pt"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="Martini"
        android:textColor="@color/white"
        android:textSize="24dp"
        android:textStyle="bold" />

</LinearLayout>

在我的 Kotlin 代码中,我有一个工作标准 adpater 和 listView 元素 Kotlin 代码

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {

        val drink= listOFDrinks[p0]
        var inflator = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var myView = inflator.inflate(R.layout.drinki_ticket,null)
        myView.tvName.text= drink.name!!
        myView.ivDrinkImage.setImageResource(drink.image!!)
        val face=Typeface.createFromAsset(assets, "Fonts/Thin.ttf")
        myView.tvName.setTypeface(face)
        return myView

    }

【问题讨论】:

  • 您正在尝试更改 textview tvname 的字体对吗??
  • 这正是我的意思
  • 我有一个错误,代码无法识别命令“getAssets()”和“assets”,我更新了帖子-
  • @YoungCodingStudent 检查我的自定义字体设置答案。

标签: android listview kotlin


【解决方案1】:

Kotlin中你可以做这样的事情。

myView.tvName.typeface = Typeface.DEFAULT_BOLD

对于自定义字体,您可以使用 ResourcesCompat.getFont,如下所示。

val myCustomFont : Typeface? = ResourcesCompat.getFont(this, R.font.custom_font)
myView.tvName.typeface = myCustomFont

来自 Assets 文件夹。

val typeface = Typeface.createFromAsset(getContext().assets, "font/custom_font.ttf")
myView.tvName.typeface = typeface

【讨论】:

  • 我什么时候在资产中有字体?
【解决方案2】:

要改变textview的字体,在adapter中写如下代码:

Typeface face = Typeface.createFromAsset(getAssets(),
            "fonts/helloworld.ttf");
myView.tvName.setTypeface(face);

其中 helloworld.ttf 是存储在项目结构的资产文件夹中的字体文件。

【讨论】:

  • 我有一个错误,代码无法识别命令“getAssets()”和“assets”,我更新了帖子
  • try face = Typeface.createFromAsset(context.getAssets(), "fonts/helloworld.ttf");
【解决方案3】:

你可以设置字体

Typeface fType = Typeface.createFromAsset(getAssets(),"fonts/Robotic.ttf"); 
tvname.setTypeface(fType);

要执行上述操作,您需要在 assets 下创建字体并在 fonts 下保留 .ttf 文件。

还有一种方法可以在 res 目录下创建 fonts 文件夹并将 ttf 文件保存在其中。

然后在你的xml中

<TextView
    android:id="@+id/tvName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:text="Martini"
    android:textColor="@color/white"
    android:textSize="24dp"
    **android:fontFamily="@font/Robotic.ttf"**
    android:textStyle="bold" />

【讨论】:

  • 我有一个错误,代码无法识别命令“getAssets()”和“assets”,我更新了帖子-
  • @YoungCodingStudent try fType = Typeface.createFromAsset(context.getAssets(), "fonts/Robotic.ttf");
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多