API 8的getNumColumns()

Nul*_*ter 1 android gridview

getNumColumns()for GridViewAPI自API 11起可用,有没有办法在API 8中获得此方法?

elg*_*gui 7

在这个重复的帖子中,有人找到了适用于API 8的答案:

private int getNumColumnsCompat() {
    if (Build.VERSION.SDK_INT >= 11) {
        return getNumColumnsCompat11();

    } else {
        int columns = 0;
        int children = getChildCount();
        if (children > 0) {
            int width = getChildAt(0).getMeasuredWidth();
            if (width > 0) {
                columns = getWidth() / width;
            }
        }
        return columns > 0 ? columns : AUTO_FIT;
    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private int getNumColumnsCompat11() {
    return getNumColumns();
}
Run Code Online (Sandbox Code Playgroud)