有时a.getIndexCount()返回0?

时间:2016-09-19 11:17:35

标签: java android

我是android的新手,我使用java功能的自定义属性。它的工作正常,除了一些小部件无法正常工作。 请参考代码如下。

AppTextview.java

private void initTextConfiguration(Context context, AttributeSet attrs)
{
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AppTextView);
    int N = a.getIndexCount();
    for (int i = 0; i < N; ++i) {
        int attr = a.getIndex(i);
        switch (attr) {
            case R.styleable.AppTextView_malyalum_text:
                malyalamtext = a.getString(attr);
            break;
            case R.styleable.AppTextView_malyalum_font:
                malyalamfontname = a.getString(attr);
            break;
            case R.styleable.AppTextView_font_name:
                fontname = a.getString(attr);
            break;
            case R.styleable.AppTextView_malayalam_text_size:
                  malyalamtextsize = a.getDimension(attr,0);
            break;
            }
    }
}

attrs.xml

  <declare-styleable name="AppTextView">
    <attr name="font_name" format="string"></attr>
    <attr name="malyalum_font" format="string"></attr>
    <attr name="malyalum_text" format="string"></attr>
    <attr name="malayalam_text_size" format="dimension"></attr>
</declare-styleable>

Layout.xml代码

    <com.app.kochione.appwidget.AppTextView
                android:id="@+id/tourism_activity"
                app:malyalum_text="@string/mal_activities"
                app:malayalam_text_size="@dimen/mal_activity_textsize"
                app:malyalum_font="@string/akhand_malayalam_semibold_font"
                app:font_name="@string/titillium_web_semibold"
                android:layout_marginLeft="@dimen/margin_ten"
                android:layout_marginTop="@dimen/margin_nine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/activity"
                android:textColor="@color/black"
                android:textSize="@dimen/margin_fifteen"
                />


        <com.app.kochione.appwidget.AppTextView
        android:id="@+id/tourism_filter"
        app:malyalum_text="@string/mal_filter"
        app:malayalam_text_size="@dimen/mal_filter_textsize"
        app:malyalum_font="@string/akhand_malayalam_semibold_font"
        android:layout_marginLeft="@dimen/margin_twenty"
        android:layout_centerVertical="true"
        android:text="@string/filter"
        style="@style/width_height_ww"
        android:textColor="@android:color/black"
        android:textSize="@dimen/fiter_textsize"
        app:font_name="@string/titillium_web_semibold" />

在AppTextiview中,a.getIndexCount()总是返回0;

0 个答案:

没有答案