我是java和android的新手,我面临的问题是我无法改变另一种布局的字体。
好吧,我有一个ListView
,我使用ArrayAdapter
将另一个布局连接到我的ListView
。像图像波纹管:
我将我的字体复制到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 );
一旦我运行这个我得到错误,我猜是因为我将setTypeface
设置为连接到ListView
的布局,但是如果我setTypeface
到TextView
那么在我的activity_main
中,它会完美运作。像图像波纹管。
所以我错过了什么?我该如何解决这个问题?
Edit_1:此处为food_row
布局的xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/food_preview"
android:id="@+id/image_food"
android:layout_width="100dp"
android:layout_height="100dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/text_food_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/text_food_description" />
</LinearLayout>
</LinearLayout>
这里是activity_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>
更新
答案 0 :(得分:2)
您需要创建自己的自定义适配器,并在适配器的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;
}
如果您不知道如何创建自定义适配器,那么您可以使用this库,它很直接
在gradle.build
dependencies {
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
}
在名为fonts的资源文件夹中创建一个文件夹,例如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
预览中实时预览字体,但您可以通过在模拟或设备中运行您的应用来检查它。
答案 1 :(得分:1)
我建议您修复部分代码: