Java 更改选项卡时自定义视图会更改
当我更改选项卡并返回到我的自定义视图所在的位置时,我的自定义视图(这是一个按钮)似乎被重新初始化 更改选项卡之前(启动应用程序时): 更改后: 在下面找到我的自定义视图的代码(不带自定义公共方法):Java 更改选项卡时自定义视图会更改,java,android,android-fragments,android-custom-view,Java,Android,Android Fragments,Android Custom View,当我更改选项卡并返回到我的自定义视图所在的位置时,我的自定义视图(这是一个按钮)似乎被重新初始化 更改选项卡之前(启动应用程序时): 更改后: 在下面找到我的自定义视图的代码(不带自定义公共方法): 我不知道发生了什么,到目前为止,我还没有找到任何正确的答案。你能展示一下你是如何更改选项卡的吗?我建议在你设置mImageViewArrow的onMeasure高度的行上设置一个断点来调试它。您可能会发现getPercent(mTextViewButton.getMeasuredHeight()
我不知道发生了什么,到目前为止,我还没有找到任何正确的答案。你能展示一下你是如何更改选项卡的吗?我建议在你设置
mImageViewArrow
的onMeasure
高度的行上设置一个断点来调试它。您可能会发现getPercent(mTextViewButton.getMeasuredHeight(),60)
没有返回您期望的值。另外,请分享您的getPercent
方法的代码。@Ovidiu我添加了getPercent
函数,然而,我认为问题来自这样一个事实,即只有在启动活动时才调用onMeasure
方法,而不是在返回到tab@NayMak这是正确的-onMeasure
将不再被调用,但是onDraw
将被调用!是否在应用程序中的任何其他位置使用了mImageViewArrow
上的绘图功能?例如,如果您有两个使用同一可绘制资源的ImageView,并且在其中一个ImageView中对可绘制应用更改,则在另一个ImageView中也会对可绘制应用应用应用相同的更改。@NayMak这可能也是您的场景中发生的情况-当您第一次打开应用程序时,将测量并绘制自定义视图。然后在其他选项卡中测量具有相同可绘制性的另一个自定义视图。返回到第一个选项卡时,将使用来自不同视图的最新测量值重新绘制自定义视图。在调用mutate()
之后,您可以通过编程而不是从XML来设置可绘制的,也可以记住自定义视图中mImageViewArrow
的第一个测量高度,然后在onDraw
中重新应用它
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;
}
}
}