TextView setVisibility(View.GONE)不会删除我的CustomTextView

时间:2013-11-15 09:01:06

标签: android visibility textview android-custom-view

我正在使用自定义textview,我试图在某些情况下隐藏它...但是当我使用方法setVisibility(View.GONE)时,我在某些设备(Nexus 4)上遇到奇怪的行为,视图不是隐藏的,而是它应该是,但它加载了来自另一个先前的textview的数据。

这是我的代码:

<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);
                }
            }
        }
    }

任何可以解决这个奇怪问题的提示?

1 个答案:

答案 0 :(得分:1)

试试这个:代替View.GONE使用View.INVISIBLE