0

我在自定义布局中遇到问题,如何在灰色上设置高度TextView以使其看起来与黄色相同TextView。黄色TextView允许多行,但灰色仅支持单行。在我的自定义布局下方。PS 我直接在TextView Thx 上设置了圆形背景。

在此处输入图像描述

public class LayoutSkillChips extends ViewGroup {

    private TextView mSkillName;
    private TextView mSkillCounter;

    public LayoutSkillChips(Context context) {
        super(context);
        initialize(context);
    }

    public LayoutSkillChips(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize(context);
    }

    public LayoutSkillChips(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context);
    }

    private void initialize(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_skill_chips, this, true);
        mSkillName = (TextView) findViewById(R.id.skill_name);
        mSkillCounter = (TextView) findViewById(R.id.skill_counter);
    }

    private void layoutView(View view, int left, int top, int width, int height) {
        MarginLayoutParams margins = (MarginLayoutParams) view.getLayoutParams();
        final int leftWithMargins = left + margins.leftMargin;
        final int topWithMargins = top + margins.topMargin;

        view.layout(leftWithMargins, topWithMargins,
                leftWithMargins + width, topWithMargins + height);
    }

    private int getWidthWithMargins(View child) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
        return child.getWidth() + lp.leftMargin + lp.rightMargin;
    }

    private int getHeightWithMargins(View child) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
        return child.getHeight() + lp.topMargin + lp.bottomMargin;
    }

    private int getMeasuredWidthWithMargins(View child) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
        return child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
    }

    private int getMeasuredHeightWithMargins(View child) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
        return child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int widthUsed = 0;
        int heightUsed = 0;

        measureChildWithMargins(mSkillCounter, widthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
        widthUsed += getMeasuredWidthWithMargins(mSkillCounter);

        measureChildWithMargins(mSkillName, widthMeasureSpec, widthUsed, heightMeasureSpec, heightUsed);
        heightUsed += getMeasuredHeightWithMargins(mSkillName);

        int heightSize = heightUsed + getPaddingTop() + getPaddingBottom();
        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int paddingLeft = getPaddingLeft();
        final int paddingTop = getPaddingTop();

        int contentLeft = paddingLeft;

        layoutView(mSkillName, contentLeft, paddingTop, mSkillName.getMeasuredWidth(), mSkillName.getMeasuredHeight());
        contentLeft += getMeasuredWidthWithMargins(mSkillName);

        layoutView(mSkillCounter, contentLeft, paddingTop, mSkillCounter.getMeasuredWidth(), mSkillCounter.getMeasuredHeight());
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new MarginLayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
}
4

1 回答 1

0

在运行时获得视图的确切高度,更好地使用

view.post(new Runnable() {

      @Override
      public void run() {
            view.getLayoutParams().height;
      }

});

您可以使用 LayoutParams 为其他人设置该高度。

于 2016-06-06T01:22:41.627 回答