2017-06-28 22 views
-1

我想展開點擊+按鈕的視圖,此+符號更改爲 - 當視圖展開時。再次點擊 - 按鈕時視圖應該縮小。 在視圖裏面我有一些TextView字段。請任何人建議我。我是新來的android。如何在android中創建可展開的視圖?

+2

爲什麼不在谷歌搜索? – Nambi

+0

我搜索但沒找到任何地方,因爲我預期 –

+0

歡迎來到StackOverflow。請參考[tour](http://stackoverflow.com/tour)瀏覽並閱讀[幫助中心](http://stackoverflow.com/help),然後閱讀[如何提問] (http://stackoverflow.com/help/how-to-ask),[我應該避免問什麼類型的問題?](http://stackoverflow.com/help/dont-ask)並提供[MCVE:最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。如果周圍的人可以輕鬆地閱讀和理解你的意思,或者問題是什麼,他們會更願意幫助:) – Dwhitz

回答

0

answer解決問題的很好的例子。

public class ExpandableTextView extends TextView implements OnClickListener 
{ 
private static final int MAX_LINES = 5; 
private int currentMaxLines = Integer.MAX_VALUE; 

public ExpandableTextView(Context context) 
{ 
    super(context); 
    setOnClickListener(this); 
} 
public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) 
{ 
    super(context, attrs, defStyle); 
    setOnClickListener(this); 
} 

public ExpandableTextView(Context context, AttributeSet attrs) 
{ 
    super(context, attrs); 
    setOnClickListener(this); 
} 

@Override 
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) 
{ 
    /* If text longer than MAX_LINES set DrawableBottom - I'm using '...' icon */ 
    post(new Runnable() 
    { 
     public void run() 
     { 
      if (getLineCount()>MAX_LINES) 
       setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_more_text); 
      else 
       setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0); 

      setMaxLines(MAX_LINES);    
     } 
    }); 
} 


@Override 
public void setMaxLines(int maxLines) 
{ 
    currentMaxLines = maxLines; 
    super.setMaxLines(maxLines); 
} 

/* Custom method because standard getMaxLines() requires API > 16 */ 
public int getMyMaxLines() 
{ 
    return currentMaxLines; 
} 

@Override 
public void onClick(View v) 
{ 
    /* Toggle between expanded collapsed states */ 
    if (getMyMaxLines() == Integer.MAX_VALUE) 
     setMaxLines(MAX_LINES); 
    else 
     setMaxLines(Integer.MAX_VALUE); 
} 

} 
0

你可以看到或消失按鈕點擊事件特定佈局在運行時像下面的代碼:

findViewById(R.id.yourButtonId).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mainView.setVisibility(View.GONE); 
      } 
     }); 
+0

但這並不如我所料。 –

+0

那麼你想要什麼? –

相關問題