自定义视图的水平中心

时间:2015-10-08 11:50:12

标签: android android-layout

对于我的应用,我使用https://github.com/namito/TagCloudLinkView/blob/master/lib/src/main/java/com/ns/developer/tagview/widget/TagCloudLinkView.java 中的代码(第263行是绘图发生的地方)来创建标记云。唯一的问题是标签对齐到屏幕的左侧。如何使标签居中?

标签现在看起来像是,与左边对齐: enter image description here

修改

我现在使用LinearLayouts来做这个伎俩,它有效,但边距被忽略了。我该如何解决这个问题?

我在循环中使用的代码:

    LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, tagHeight);

    LayoutParams tagParams = new LayoutParams(HEIGHT_WC, HEIGHT_WC);

    if (mWidth <= total + tagWidth + LAYOUT_WIDTH_OFFSET) {
        if(listId != 9000) {
            linearLayoutParams.addRule(RelativeLayout.BELOW, lastLayoutId);
        }
        listId++;
        linearLayoutParams.topMargin = TAG_LAYOUT_TOP_MARGIN;
        addView(linearLayout, linearLayoutParams);
        lastLayoutId = linearLayout.getId();

        linearLayout = new LinearLayout(getContext());
        linearLayout.setId(++listId);
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        tagParams = new LayoutParams(HEIGHT_WC, HEIGHT_WC);
        tagParams.setMargins(TAG_LAYOUT_LEFT_MARGIN, 0, TAG_LAYOUT_LEFT_MARGIN, 0);

        total = getPaddingLeft() + getPaddingRight();
        pindex = index;
    } else {
        tagParams.addRule(RelativeLayout.ALIGN_TOP, pindex);
        tagParams.addRule(RelativeLayout.RIGHT_OF, index - 1);

    }

    tagParams.setMarginStart(TAG_LAYOUT_LEFT_MARGIN);
    tagParams.setMarginEnd(TAG_LAYOUT_LEFT_MARGIN);
    total += TAG_LAYOUT_LEFT_MARGIN;
    total += TAG_LAYOUT_LEFT_MARGIN;

    linearLayout.addView(tagLayout, tagParams);
    total += tagWidth;

    index++;
    listIndex++;

0 个答案:

没有答案