2013-11-15 100 views
1

我正在使用自定義textview,我試圖在某些情況下隱藏它...但是,當我在某些設備(Nexus 4)上時出現奇怪的行爲使用方法setVisibility(View.GONE),視圖不會被隱藏,因爲它應該是加載了來自另一個以前的textview的數據。TextView setVisibility(View.GONE)不會刪除我的CustomTextView

這裏是我的代碼:

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

,可以解決這個奇怪的問題任何提示?

+0

「deleteBtnVisible」 什麼是什麼? – amalBit

+0

它是一個布爾值,告訴我是否需要顯示我的文本視圖 –

+0

View.GONE應該確保您的TextView不可見,並且在佈局中不佔用空間 – vamsiampolu

回答

1

試試這個:在地方View.GONE的使用View.INVISIBLE

+0

這不是我需要的功能,因爲textview被放置在一個滾動,它在底部,所以我不想能夠滾動一些白色的表面...查看。就我所知,INVISIBLE不會從ViewGroup中刪除視圖... –

相關問題