【问题标题】:Custom Attributes for TextView Subclass not workingTextView 子类的自定义属性不起作用
【发布时间】:2015-09-03 06:38:58
【问题描述】:

我将 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>

'

Styled 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" />

'

所有 StyledTextView 都使用 Normal 字体,因为

中的失败案例
`style = a.getInt(R.styleable.StyledTextView_text_style, NORMAL);`

这让我相信在 xml 中没有正确检索到属性,而且我还有其他类也有在 xml 中定义的自定义属性没有效果的相同问题
我已经阅读了几个在线线程,似乎包括

xmlns:app="http://schemas.android.com/apk/res-auto" 

在根视图中是 xml 布局中唯一需要使用自定义属性的东西 如果有人可以提供帮助,将不胜感激!

【问题讨论】:

    标签: android xml textview attr


    【解决方案1】:

    我想出了答案!

    在设置样式后,我得到了类型化的数组和属性

    我变了:

    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);
        }
    

    【讨论】:

      猜你喜欢
      • 2015-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多