我正在使用自定义文本视图,我试图在某些情况下隐藏它......但是当我使用 setVisibility(View.GONE) 方法时,我在某些设备(Nexus 4)上得到了一个奇怪的行为,视图没有隐藏应该是这样,但它加载了来自另一个先前文本视图的数据。
这是我的代码:
<ro.gebs.captoom.utils.fonts.CustomFontTextView
android:id="@+id/delete_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/layout_padding"
android:background="@drawable/rounded"
android:drawablePadding="10dp"
android:drawableRight="@drawable/input_delete_red"
android:padding="@dimen/layout_padding"
android:paddingLeft="10dp"
android:text="@string/delete"
android:textColor="@color/redish"
android:textSize="18sp"
custom:fontName="SemiBold"
android:visibility="gone"/>
CustomFontTextView 类:
package ro.gebs.captoom.utils.fonts;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
import ro.gebs.captoom.R;
public class CustomFontTextView extends TextView {
public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView,
defStyle, 0);
assert a != null;
int fontId = a.getInteger(R.styleable.CustomFontTextView_fontName, -1);
if (fontId == -1) {
throw new IllegalArgumentException("The font_name attribute is required and must refer "
+ "to a valid child.");
}
a.recycle();
initialize(fontId);
}
}
public CustomFontTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomFontTextView(Context context) {
super(context);
}
@SuppressWarnings("ConstantConditions")
public void initialize(int fontId) {
Typeface tf = null;
switch (fontId) {
case -1:
case 0:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Regular.ttf");
break;
case 1:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Bold.ttf");
break;
case 2:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-Semibold.ttf");
break;
case 3:
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/OpenSans-ExtraBold.ttf");
break;
}
setTypeface(tf);
}
}
我的活动代码:
@Override
protected void onResume() {
super.onResume();
if (deleteBtnVisible) {
delete_btn.setVisibility(View.VISIBLE);
} else {
delete_btn.setVisibility(View.GONE);
}
Bundle extras = getIntent().getExtras();
if (extras != null) {
final int statusFolder = extras.getInt(Constants.STATUS, Constants.STATUS_NEW);
if (statusFolder == Constants.STATUS_EDIT) {
if (folder.getTitle().equals("Inbox")) {
delete_btn.setVisibility(View.GONE);
} else {
delete_btn.setVisibility(View.VISIBLE);
delete_btn.setEnabled(true);
}
}
}
}
有什么提示可以解决这个奇怪的问题吗?