我正在尝试克隆TextView的行为。我已经检查了有关此主题的其他线程,很明显,最好的方法是通过view.findViewById(id)方法对窗口小部件进行膨胀。但是,这种方式似乎只提供TextView静态行为的副本,即只提供XML中定义的行为。但是,我需要做的是复制以编程方式更改的TextView的行为,这里是上下文:
<TextView
android:id="@+id/text_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:fontFamily="sans-serif-light"
android:textStyle="italic"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:padding="5dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:text="Hello world"
android:scrollbars="vertical"/>
&#13;
TextView txtView = (TextView) view.findViewById(R.id.text_test);
txtView.setMovementMethod(new ScrollingMovementMethod());
&#13;
TextView[] array = new TextView[200];
for( int i = 0; i < 200; i++ ){
array[i] = (TextView) view.findViewById(R.id.text_test);
array[i].setMovementMethod( txtView.getMovementMethod() );
array[i].setEachMethodOfTextView( txtView.getEachMethodOfTextView() );
....
}
&#13;
12-31 12:38:12.165 27590-27590/example.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: example.your_app, PID: 27590
java.lang.StackOverflowError
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.objenesis.instantiator.android.Android18Instantiator.newInstance(Android18Instantiator.java:44)
at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73)
at com.rits.cloning.ObjenesisInstantiationStrategy.newInstance(ObjenesisInstantiationStrategy.java:18)
at com.rits.cloning.Cloner.newInstance(Cloner.java:271)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:436)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject(Cloner.java:453)
at com.rits.cloning.Cloner.cloneInternal(Cloner.java:431)
at com.rits.cloning.Cloner.cloneObject
&#13;
总之,我需要知道的是如何克隆/复制TextView的所有行为(更常见的是,任何类型的小部件视图),而不必逐个设置每个属性和方法。
感谢。
答案 0 :(得分:1)
嗯,我认为解决方案非常非常简单:创建一个扩展TextView的自定义视图,并在CustomView的构造函数中设置所有这些参数。然后在xml中使用此自定义视图。
你甚至可以将你在xml中设置的所有参数移动到构造函数中以简化xml(类似这样):
<your.package.view.CustomTextView
android:id="@+id/text_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />