布局在棉花糖版本之后看起来很奇怪

时间:2017-03-28 08:16:43

标签: android android-layout android-linearlayout android-6.0-marshmallow

HIII,      我正在进行自定义布局。它完美地适用于棒棒糖及以下版本的Android,但后来的版本显示出奇怪的外观。

我想要并在< = Android 5中显示

enter image description here

它是如何在> Android 5中显示的 enter image description here

这是布局结构的草图布局。 enter image description here

以下是代码

public class TableListView extends LinearLayout {

private final ViewGroup headerLinearLayout;

public TableListView(final Context context) {
    this(context, null);
}

public TableListView(final Context context, final AttributeSet attributeSet) {
    super(context, attributeSet);

    setOrientation(LinearLayout.VERTICAL);

    final ViewsCreator viewsCreator = new ViewsCreator();
    headerLinearLayout = viewsCreator.createHeaderViews(context, attributeSet);
    columnNameIndices = viewsCreator.getColumnNameIndexMap();

    addView(headerLinearLayout);
    addView(listView);
}

private static class ViewsCreator {
    private final Map<String, Integer> columnNameIndices = new HashMap<String, Integer>();

    private ViewsCreator() {
    }

    public ViewGroup createHeaderViews(final Context context, final AttributeSet attributeSet) {
        final HeaderView.HeaderParams headerParams = new HeaderView.HeaderParams(context, attributeSet);
        final LinearLayout headerLinearLayout = new LinearLayout(context);
        headerLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

        final int numberOfColumns = headerParams.getNumOfColumns();

        for (int i = 0; i < numberOfColumns; ++i) {
            final String headerName = headerParams.headerNames[i].toString();
            headerLinearLayout.addView(new HeaderView(context, headerParams, i, headerName));
            columnNameIndices.put(headerName, i);
        }

        final ViewTreeObserver viewTreeObserver = headerLinearLayout.getViewTreeObserver();
        final ViewsCreatorOnPreDrawListener preDrawListener = new ViewsCreatorOnPreDrawListener(headerLinearLayout);
        viewTreeObserver.addOnPreDrawListener(preDrawListener);

        return headerLinearLayout;
    }

    public Map<String, Integer> getColumnNameIndexMap() {
        return columnNameIndices;
    }

    private static class ViewsCreatorOnPreDrawListener implements ViewTreeObserver.OnPreDrawListener {
        private final ViewGroup viewGroup;
        private int largestHeaderHeight = 0;

        public ViewsCreatorOnPreDrawListener(final ViewGroup viewGroup) {
            this.viewGroup = viewGroup;
        }

        @Override
        public boolean onPreDraw() {
            final ViewTreeObserver viewTreeObserver = viewGroup.getViewTreeObserver();

            viewTreeObserver.removeOnPreDrawListener(this);

            final int childCount = viewGroup.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                final View childHeaderView = viewGroup.getChildAt(i);

                final int height = childHeaderView.getHeight();
                if (largestHeaderHeight < height) {
                    largestHeaderHeight = height;
                }
            }

            for (int i = 0; i < childCount; ++i) {
                final View childHeaderView = viewGroup.getChildAt(i);

                final ViewGroup.LayoutParams layoutParams = childHeaderView.getLayoutParams();
                layoutParams.height = largestHeaderHeight;
            }

            return false;
        }
    }
}

/**
 * Represents a header in this {@link TableListView}.
 */
private static class HeaderView extends RelativeLayout {
    private ImageView ivIcon;
    private TextView tvName;
    private ImageView ivArrow;

    public HeaderView(final Context context, final HeaderParams headerParams, final int index, final String name) {
        super(context);
        setGravity(Gravity.CENTER);
        setBackgroundResource(headerParams.headerBackground);
        setLayoutParams(
                new LinearLayout.LayoutParams(0, headerParams.headerHeight, headerParams.headerWeights[index]));

        addNameView(context, headerParams, name);
        addArrowView(context, headerParams);
        addIconView(context, headerParams, index);
    }

    private void addArrowView(final Context context, final HeaderParams headerParams) {
        ivArrow = new ImageView(context);
        this.addView(ivArrow);
        ivArrow.setId(R.id.iv_header_arrow);
        final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivArrow.getLayoutParams();
        params.addRule(RelativeLayout.RIGHT_OF, android.R.id.title);
        params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
        params.leftMargin = context.getResources().getDimensionPixelSize(R.dimen.pixel_2dp);
        params.width = context.getResources().getDimensionPixelSize(R.dimen.pixel_9dp);
        params.height = context.getResources().getDimensionPixelSize(R.dimen.pixel_8dp);
        ivArrow.setScaleType(ImageView.ScaleType.FIT_XY);
        ivArrow.setLayoutParams(params);
        setArrowResIds(headerParams.headerArrowUp, headerParams.headerArrowDown);
    }

    private void addIconView(final Context context, final HeaderParams headerParams, final int index) {
        if (headerParams.headerIconIndexes.indexOf(index) >= 0) {
            ivIcon = new ImageView(context);
            this.addView(ivIcon);
            ivIcon.setImageResource(headerParams.headerIcon);
            final LayoutParams params = (LayoutParams) ivIcon.getLayoutParams();
            params.addRule(RelativeLayout.LEFT_OF, android.R.id.title);
            params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
            params.rightMargin = context.getResources().getDimensionPixelSize(R.dimen.pixel_2dp);
            ivIcon.setLayoutParams(params);
        }
    }

    private void addNameView(final Context context, final HeaderParams headerParams, final String name) {
        tvName = new TextView(context);
        this.addView(tvName);
        tvName.setId(android.R.id.title);
        tvName.setText(name);
        tvName.setTextColor(context.getResources().getColor(headerParams.headerTextColor));
        tvName.setTextSize(TypedValue.COMPLEX_UNIT_PX, headerParams.headerTextSize);
        if (!TextUtils.isEmpty(headerParams.headerTypeface)) {
            final Typeface typeface =
                    FontManager.getInstance().getFontByName(headerParams.headerTypeface.toString());
            tvName.setTypeface(typeface, Typeface.BOLD);
        } else {
            tvName.setTypeface(Typeface.DEFAULT_BOLD);
        }
        final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tvName.getLayoutParams();
        params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        tvName.setGravity(Gravity.CENTER);
        tvName.setLayoutParams(params);
    }

    private static class HeaderParams {
        private final CharSequence[] headerNames;
        private final float[] headerWeights;
        private final int headerHeight;
        private final int headerBackground;
        private final int headerArrowUp;
        private final int headerArrowDown;
        private final int headerIcon;
        private final List<Integer> headerIconIndexes = new ArrayList<>();
        private final int headerTextColor;
        private final CharSequence headerTypeface;
        private final int headerTextSize;

        public HeaderParams(final Context ctx, final AttributeSet attributeSet) {
            final TypedArray typedArray = ctx.obtainStyledAttributes(attributeSet, R.styleable.TableListView);

            headerNames = typedArray.getTextArray(R.styleable.TableListView_headerNameArray);

            final CharSequence[] weightsArr = typedArray.getTextArray(R.styleable.TableListView_headerWeightArray);
            headerWeights = new float[weightsArr.length];
            for (int i = 0; i < weightsArr.length; i++) {
                headerWeights[i] = Float.valueOf(weightsArr[i].toString());
            }

            headerHeight = typedArray.getDimensionPixelSize(R.styleable.TableListView_headerHeight,
                                                            ViewGroup.LayoutParams.WRAP_CONTENT);
            headerBackground = typedArray.getResourceId(R.styleable.TableListView_headerBackground, 0);
            headerArrowUp = typedArray.getResourceId(R.styleable.TableListView_headerArrowUp, 0);
            headerArrowDown = typedArray.getResourceId(R.styleable.TableListView_headerArrowDown, 0);
            headerIcon = typedArray.getResourceId(R.styleable.TableListView_headerIcon, 0);

            final CharSequence[] iconIndexes = typedArray.getTextArray(R.styleable.TableListView_headerIconIndexes);
            if (iconIndexes != null) {
                for (final CharSequence iconIndex : iconIndexes) {
                    headerIconIndexes.add(Integer.valueOf(iconIndex.toString()));
                }
            }

            headerTextColor = typedArray.getResourceId(R.styleable.TableListView_headerTextColor, R.color.white);
            headerTypeface = typedArray.getText(R.styleable.TableListView_headerTypeface);

            final int defSize = ctx.getResources().getDimensionPixelSize(R.dimen.pixel_8dp);
            headerTextSize = typedArray.getDimensionPixelSize(R.styleable.TableListView_headerTextSize, defSize);
            typedArray.recycle();
        }
    }
}
}

需要帮助。

0 个答案:

没有答案