2012-11-29 110 views
1

说我有一个从XML获得的textview。如何克隆TextView?

final View popupView = getLayoutInflater().inflate(R.layout.popup, null); 

final TextView tvPop = (TextView)popupView.findViewById(R.id.tvKeyPop); 

但我想有多个tvPop的,我应该如何克隆呢?

谢谢。

回答

0

,你可以这样做:

TextView cloned = new TextView(getApplicationContext()); 
cloned.setText(tvPop.getText()); 
cloned.setLayoutParams(tvPop.getLayoutParams()); 
... 
cloned.setWhateverFieldYouNeedToBeCloned(tvPop().getWhateverFieldYouNeedToBeCloned()); 

希望有所帮助。

+0

有没有方便的方法? – suitianshi

+1

真的很老的线程,但只是为了编辑。您应该传递一个活动上下文而不是应用程序上下文来防止泄漏。 – hadez30