0

如何android.view.InflateException在将布局 XML 文件实例化为相应的 View 对象时自然地调用?throw禁止使用 AttributeSet 参数擦除构造函数。

使用一组典型的构造函数:

public TestView(Context context) {
    this(context, null);
}

public TestView(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.gridViewStyle);
}

public TestView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedArray a = context.obtainStyledAttributes(attrs,
            com.android.internal.R.styleable.GridView, defStyle, 0);

    int hSpacing = a.getDimensionPixelOffset(
            com.android.internal.R.styleable.GridView_horizontalSpacing, 0);
    setHorizontalSpacing(hSpacing);

    int vSpacing = a.getDimensionPixelOffset(
            com.android.internal.R.styleable.GridView_verticalSpacing, 0);
    setVerticalSpacing(vSpacing); 

    int index = a.getInt(com.android.internal.R.styleable.GridView_stretchMode, STRETCH_COLUMN_WIDTH);
    if (index >= 0) {
        setStretchMode(index);
    }

    int columnWidth = a.getDimensionPixelOffset(com.android.internal.R.styleable.GridView_columnWidth, -1);
    if (columnWidth > 0) {
        setColumnWidth(columnWidth);
    }

    int numColumns = a.getInt(com.android.internal.R.styleable.GridView_numColumns, 1);
    setNumColumns(numColumns);

    index = a.getInt(com.android.internal.R.styleable.GridView_gravity, -1);
    if (index >= 0) {
        setGravity(index);
    }

    a.recycle();
}
4

1 回答 1

0

由于InflateException继承自 RuntimeException 你可以自由地抛出它

于 2014-03-12T10:01:30.623 回答