Java 在Android中更改TextView的字体

Java 在Android中更改TextView的字体,java,android,android-studio,Java,Android,Android Studio,我对java和android非常陌生,我面临的问题是我无法更改其他版面的字体 好的,我有一个列表视图,我使用阵列适配器将另一个布局连接到我的列表视图。如下图所示: 我将字体复制到assets文件夹中,并将其写入onCreate: // list of titles String[] pizza_titles = {"BBQ Bacon Cheeseburger", "Old-Fashioned Meatbrawl", "7-Alarm Fire", "Sweet Sriracha Dynami

我对java和android非常陌生,我面临的问题是我无法更改其他版面的字体

好的,我有一个
列表视图
,我使用
阵列适配器
将另一个布局连接到我的
列表视图
。如下图所示:

我将字体复制到
assets
文件夹中,并将其写入
onCreate

// list of titles
String[] pizza_titles = {"BBQ Bacon Cheeseburger", "Old-Fashioned Meatbrawl", "7-Alarm Fire", "Sweet Sriracha Dynamite","Cock-A-Doodle Bacon"};

// getting the ListView ID
pizza_list_view = (ListView) findViewById(R.id.listView_Pizza);

// creating ArrayAdapter and connecting the layout to ListView as well as list of string to a TextView 
ArrayAdapter<String> adapter=new ArrayAdapter<String (this,R.layout.food_row,R.id.text_food_title,pizza_titles);
pizza_list_view.setAdapter(adapter);

// defining the font typeface
Typeface pizza_title_font = Typeface.createFromAsset(getAssets(),"pizza.ttf");
TextView pizza_title = (TextView) findViewById(R.id.text_food_title);
pizza_title.setTypeface(pizza_title_font );
这里是
活动\u main
xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="domain.bernard.task_02.MainActivity"
    android:weightSum="1">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="317dp"
        android:id="@+id/listView_Pizza"
        android:layout_gravity="center_horizontal" />

    <TextView
        android:layout_width="302dp"
        android:layout_height="48dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView2" />

</LinearLayout>

更新:


我建议修复部分代码:

  • 您在根布局中定义了weightSum,但未在子布局中指定布局权重
  • 对于使用自定义字体,我真的建议使用库
  • 正如Andres所建议的那样,使用自定义适配器代替ArrayAdapter,以增强自定义功能

我建议修复部分代码:

  • 您在根布局中定义了weightSum,但未在子布局中指定布局权重
  • 对于使用自定义字体,我真的建议使用库
  • 正如Andres所建议的那样,使用自定义适配器代替ArrayAdapter,以增强自定义功能

您需要创建自己的自定义适配器,并在适配器的
getView
中设置字体,以下是示例

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    if (convertView == null)
        vi = lif.inflate(R.layout.inflate, null);
    imageView = (ImageView) vi.findViewById(R.id.imageForImageView);

    TextView pizza_title = (TextView) findViewById(R.id.text_food_title);
    pizza_title.setText("Your text");

    final Typeface pizza_title_font  = Typeface.createFromAsset(getAssets(),"pizza.ttf");
    pizza_title.setTypeface(pizza_title_font);

    return vi;
}
若你们不知道如何创建自定义适配器,那个么你们可以使用这个库,这很简单

在您的
gradle.build中包含依赖项

dependencies {
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}
在资产文件夹中创建一个名为字体的文件夹,例如
assets/fonts
,然后将字体移动到该文件夹中,然后移动到
TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    fontPath="fonts/pizza.ttf"
    android:id="@+id/text_food_title" />


请记住:它不会在
XML
预览中为您提供字体的实时预览,但您可以通过在仿真或设备中运行应用程序进行检查。

您需要创建自己的自定义适配器,并在适配器的
getView
中设置字体,以下是示例

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    if (convertView == null)
        vi = lif.inflate(R.layout.inflate, null);
    imageView = (ImageView) vi.findViewById(R.id.imageForImageView);

    TextView pizza_title = (TextView) findViewById(R.id.text_food_title);
    pizza_title.setText("Your text");

    final Typeface pizza_title_font  = Typeface.createFromAsset(getAssets(),"pizza.ttf");
    pizza_title.setTypeface(pizza_title_font);

    return vi;
}
若你们不知道如何创建自定义适配器,那个么你们可以使用这个库,这很简单

在您的
gradle.build中包含依赖项

dependencies {
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}
在资产文件夹中创建一个名为字体的文件夹,例如
assets/fonts
,然后将字体移动到该文件夹中,然后移动到
TextView

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    fontPath="fonts/pizza.ttf"
    android:id="@+id/text_food_title" />


请记住:它不会在
XML
preview中为您提供字体的实时预览,但您可以通过在仿真或设备中运行应用程序进行检查。

放入适配器代码(XML和java)您需要一个自定义适配器将
字体设置为
文本视图
@Amir我已经更新了我的问题,
食物行
是唯一连接到
列表视图
@Anders自定义适配器的布局?我该怎么做呢?也请输入您的错误:)输入适配器代码(xml和java)您需要一个自定义适配器来将
字体设置为
文本视图
@Amir我已经更新了我的问题,
食物行
是唯一连接到
列表视图
@Anders自定义适配器的布局?我该怎么做呢?请同时输入错误:)如何在自定义适配器中加载
createFromAsser
getAssets()
?它一直给我错误。这是一个例子,您需要上下文来获取资产,比如
getContext().getAssets()
您是如何在自定义适配器中为
createFromAsser
加载
getAssets()
的?它一直给我错误。这是一个例子,您需要上下文来获取资产,类似于
getContext().getAssets()