如何克隆/复制已以编程方式更改的TextView对象的行为?

时间:2014-12-31 17:57:51

标签: android android-widget

我正在尝试克隆TextView的行为。我已经检查了有关此主题的其他线程,很明显,最好的方法是通过view.findViewById(id)方法对窗口小部件进行膨胀。但是,这种方式似乎只提供TextView静态行为的副本,即只提供XML中定义的行为。但是,我需要做的是复制以编程方式更改的TextView的行为,这里是上下文:

  1. 下面是我的XML。为了简单起见,我只关注一件事:使TextView可滚动(android:scrollbars =" vertical")但请记住,我需要在TextView对象上执行比这更复杂的行为
  2. 
    
    <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;
    &#13;
    &#13;

    1. 为了使TextView可滚动,我还必须在Renderer类的inflate方法中包含这两行:
    2. &#13;
      &#13;
      TextView txtView = (TextView) view.findViewById(R.id.text_test);
      txtView.setMovementMethod(new ScrollingMovementMethod());
      &#13;
      &#13;
      &#13;

      1. 现在,我需要做的是创建上面的txtView对象的多个副本(大约200个)。如果我只使用findViewById方法来扩展窗口小部件,它将创建一个新的TextView,其中包含我的XML中定义的属性,但它不会包含我通过setMovementMethod设置为txtView的ScrollingMovementMethod对象的引用,所以我必须以编程方式进行,例如:
      2. &#13;
        &#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;
        &#13;
        &#13;

        1. 现在,问题在于我不仅需要复制移动方法,还需要复制在txtView上动态更改的所有类型的侦听器和属性。如果我遵循与前一步骤相同的逻辑,我将不得不遵循一种繁琐而不优雅的方式,逐个复制来自基础对象(txtView)的每个属性,这些属性已经过编程和动态更改。所以,而不是那样,我尝试使用深度克隆技术(使用谷歌guava库),但我得到了一些StackOverflow错误,因为TextView对象包含更复杂的对象和嵌套引用。这是日志跟踪:
        2. &#13;
          &#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;
          &#13;
          &#13;

          总之,我需要知道的是如何克隆/复制TextView的所有行为(更常见的是,任何类型的小部件视图),而不必逐个设置每个属性和方法。

          感谢。

1 个答案:

答案 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" />