我想解决的问题是在Android应用程序中使用自定义字体。我已经遵循了一些教程和堆栈溢出问题,但我似乎无法得到我想要的。
我在attrs.xml中定义了一个自定义属性:
<resources>
<declare-styleable name="CustomTextView">
<attr name="customTypeface" format="string"/>
</declare-styleable>
以及样式中定义的customTypeface:
<style name="posTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:textViewStyle">@style/posThemeTextViewStyle</item>
</style>
<style name="posThemeTextViewStyle" parent="android:Widget.TextView">
<item name="android:textSize">50sp</item>
<item name="customTypeface">Fonts/MuseoSans_100.otf</item>
</style>
在活动中,表单看起来像:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOL OL U MAD BRO!"
/>
<myApplication.Controls.CustomTextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOL OL U MAD BRO!"
></myApplication.Controls.iQTextView>
</LinearLayout>
当我尝试获取属性时:
public CustomTextView(Context context, IAttributeSet attrs, int defStyle)
: base(context, attrs, defStyle)
{
var a = context.ObtainStyledAttributes(attrs,
Resource.Styleable.CustomTextView);
var customFont = a.GetString(Resource.Styleable.CustomTextView_customTypeface);
SetCustomFont(customFont);
a.Recycle();
}
customFont为null,除非我在活动中隐式定义custom:customTypeface =“”,但我不想为控件的每个单个实例定义字体。
我错过了什么?
答案 0 :(得分:0)
不应该是这两行:
var a = context.ObtainStyledAttributes(attrs,
Resource.Styleable.CustomTextView);
var customFont = a.GetString(Resource.Styleable.CustomTextView_customTypeface);
是:
var a = context.ObtainStyledAttributes(attrs,
R.Styleable.CustomTextView);
var customFont = a.GetString(R.Styleable.CustomTextView_customTypeface);