我将TextView子类化为创建我自己的TextView,它使用自定义字体:
我的自定义textview类
`public class StyledTextView extends TextView {
int style;
static final int BOLD = 0;
static final int SEMIBOLD = 1;
static final int NORMAL = 2;
static final int LIGHT = 3;
public StyledTextView(Context context) {
super(context);
style(context);
}
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(context);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
a.recycle();
}
public StyledTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
style(context);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
a.recycle();
}
private void style(Context context) {
Typeface tf;
switch(style) {
case BOLD:
tf = Typeface.createFromAsset(context.getAssets(),
"fonts/open-sans.bold.ttf");
setTypeface(tf);
break;
case SEMIBOLD:
tf = Typeface.createFromAsset(context.getAssets(),
"fonts/open-sans.semibold.ttf");
setTypeface(tf);
break;
case NORMAL:
tf = Typeface.createFromAsset(context.getAssets(),
"fonts/open-sans.regular.ttf");
setTypeface(tf);
break;
case LIGHT:
tf = Typeface.createFromAsset(context.getAssets(),
"fonts/open-sans.light.ttf");
setTypeface(tf);
break;
default:
tf = Typeface.createFromAsset(context.getAssets(),
"fonts/open-sans.regular.ttf");
setTypeface(tf);
break;
}
}
}'
我的attrs文件:
'<?xml version="1.0" encoding="utf-8"?>
<declare-styleable name="StyledTextView">
<attr name="text_style" format="integer"/>
</declare-styleable>
<declare-styleable name="SlidingUpPanelLayout">
<attr name="panelHeight" format="dimension" />
<attr name="shadowHeight" format="dimension" />
<attr name="paralaxOffset" format="dimension" />
<attr name="fadeColor" format="color" />
<attr name="flingVelocity" format="integer" />
<attr name="dragView" format="reference" />
<attr name="overlay" format="boolean"/>
<attr name="anchorPoint" format="float" />
<attr name="initialState" format="enum">
<enum name="expanded" value="0" />
<enum name="collapsed" value="1" />
<enum name="anchored" value="2" />
<enum name="hidden" value="3" />
</attr>
</declare-styleable>
<declare-styleable name="ExtendedPagerSlidingTabStrip">
<attr name="indicatorColor" format="color"/>
<attr name="underlineColor" format="color"/>
<attr name="dividerColor" format="color"/>
<attr name="indicatorHeight" format="dimension"/>
<attr name="underlineHeight" format="dimension"/>
<attr name="mydividerPadding" format="dimension"/>
<attr name="tabPaddingLeftRight" format="dimension"/>
<attr name="scrollOffset" format="dimension"/>
<attr name="tabBackground" format="reference"/>
<attr name="shouldExpand" format="boolean"/>
<attr name="mytextAllCaps" format="boolean"/>
</declare-styleable>
&#39;
使用样式化TextView的示例 `
<UtilityClasses.StyledTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suggested Channels"
android:id="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_marginTop="25dp"
android:textSize="20sp"
app:text_style="0"
android:textColor="#787878"
android:textAllCaps="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp" />
&#39;
由于
中的失败案例,所有StyledTextView都使用Normal字体`style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);`
这让我相信在xml中没有正确检索属性,我还有其他类在xml中定义的自定义属性问题同样没有效果
我已经阅读了几个在线帖子,它似乎包括
xmlns:app="http://schemas.android.com/apk/res-auto"
根视图中的是xml布局中唯一需要使用自定义属性的东西 如果有人可以提供帮助,将不胜感激!
答案 0 :(得分:0)
我想出了答案!
我已经设置了样式
后获得了类型化数组和属性我改变了:
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
style(context);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
a.recycle();
}
对此:
public StyledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StyledTextView);
try {
style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);
} finally {
a.recycle();
}
style(context);
}