自定义视图在更改选项卡时发生更改

时间:2018-10-10 14:51:43

标签: java android android-fragments android-custom-view

当我更改选项卡并返回到我的自定义视图所在的位置时,我的自定义视图(这是一个按钮)似乎已重新初始化。

更改标签前(当我启动应用程序时):

enter image description here

更改后:

enter image description here

在我的自定义视图的代码下查找(没有自定义公共方法):

public class ButtonView extends RelativeLayout {

    private RelativeLayout mRelativeContainer;
    private TextView mTextViewButton;
    private ImageView mImageViewArrow;
    private DiagonalLayout mDiagonalLayout;
    private RelativeLayout mRelativeLayoutDiagonalColor;

    private int mDirection;
    private String mText;
    private float mTextSize;
    private int mTextColor;
    private int mButtonColor;
    private int mObliqueColor;

    public ButtonView(Context context) {
        super(context);
        customLabel(context, null);
        init(context);
    }

    public ButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);
        customLabel(context, attrs);
        init(context);
    }

    public ButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        customLabel(context, attrs);
        init(context);
    }

    private void init(Context context) {
        switch (mDirection) {
            case DIRECTION_RIGHT:
                inflate(context, R.layout.button_view_right, this);
                break;
            case DIRECTION_LEFT:
                inflate(context, R.layout.button_view_left, this);
                break;
        }
        mRelativeContainer = findViewById(R.id.container);
        mTextViewButton = findViewById(R.id.text);
        mImageViewArrow = findViewById(R.id.arrow);
        mDiagonalLayout = findViewById(R.id.diagonal);
        mRelativeLayoutDiagonalColor = findViewById(R.id.diagonal_color);

        mRelativeContainer.setBackgroundResource(R.drawable.bg_button_view);
        setText(mText);
        setTextSize(mTextSize);
        setTextColor(mTextColor);
        setButtonColor(mButtonColor);
        setObliqueColor(mObliqueColor);
    }

    private void customLabel(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ButtonView,
                0, 0);

        try {
            mDirection = typedArray.getInt(R.styleable.ButtonView_direction, DIRECTION_RIGHT);
            mText = typedArray.getString(R.styleable.ButtonView_text);
            mTextSize = typedArray.getDimensionPixelSize(R.styleable.ButtonView_text_size, 52);
            mTextColor = typedArray.getColor(R.styleable.ButtonView_text_color, getResources().getColor(android.R.color.white));
            mButtonColor = typedArray.getColor(R.styleable.ButtonView_button_color, getResources().getColor(R.color.blue));
            mObliqueColor = typedArray.getColor(R.styleable.ButtonView_oblique_color, getResources().getColor(R.color.dark_blue));
        } finally {
            typedArray.recycle();
        }
    }

    private int getPercent(int i, int percent) {
         return i * percent / 100;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int marginTopBottom = getPercent(mTextViewButton.getMeasuredHeight(), 50);
        int marginStartEnd = getPercent(mTextViewButton.getMeasuredHeight(), 70);

        LinearLayout.LayoutParams textViewButtonParams = (LinearLayout.LayoutParams) mTextViewButton.getLayoutParams();
        textViewButtonParams.setMargins(
                marginStartEnd, marginTopBottom,
                marginStartEnd, marginTopBottom);
        mTextViewButton.setLayoutParams(textViewButtonParams);

        /***************************************************************/

        ((LinearLayout.LayoutParams) mImageViewArrow.getLayoutParams()).width = getPercent(mTextViewButton.getMeasuredHeight(), 60);
        ((LinearLayout.LayoutParams) mImageViewArrow.getLayoutParams()).height = getPercent(mTextViewButton.getMeasuredHeight(), 60);

        switch (mDirection) {
            case DIRECTION_RIGHT:
                ((LinearLayout.LayoutParams) mImageViewArrow.getLayoutParams()).setMargins(0, 0, marginStartEnd, 0);

                mDiagonalLayout.getLayoutParams().width = getMeasuredWidth();
                mDiagonalLayout.getLayoutParams().height = getPercent(mTextViewButton.getMeasuredHeight(), 35);

                ((RelativeLayout.LayoutParams) mDiagonalLayout.getLayoutParams()).setMargins(
                        0,
                        marginTopBottom * 2 + mTextViewButton.getMeasuredHeight() - mDiagonalLayout.getLayoutParams().height,
                        0,
                        0);
                break;
            case DIRECTION_LEFT:
                ((LinearLayout.LayoutParams) mImageViewArrow.getLayoutParams()).setMargins(marginStartEnd, 0, 0, 0);

                mDiagonalLayout.getLayoutParams().width = getPercent(mTextViewButton.getMeasuredHeight(), 60) * 4;
                mDiagonalLayout.getLayoutParams().height = getPercent(mTextViewButton.getMeasuredHeight(), 35);

                ((RelativeLayout.LayoutParams) mDiagonalLayout.getLayoutParams()).setMargins(
                        0,
                        marginTopBottom * 2 + mTextViewButton.getMeasuredHeight() - mDiagonalLayout.getLayoutParams().height,
                        0,
                        0);
                break;
        }
    }
}

我不知道会发生什么,到目前为止我还没有找到合适的答案。

0 个答案:

没有答案