我曾经做过很多 Flash Actionscript,现在我正在进入 Android。Android API 中是否有类似于 Actionscript 中的 duplicateMovieClip() 的内容?我确信可能有一种方法可以编写这样的方法,但我想知道是否有任何现有的快捷方式。
例如,假设我在屏幕上有一个 ImageView、TextView 或其他类型的视图对象,并且我希望有一个可以单击的按钮,该按钮将复制屏幕上的某些对象。
如果你不介意我问,你为什么需要类似的东西duplicateMovieClip()
?
为了回答这个问题,Android 没有 AS2 的概念duplicateMovieClip()
。就像在 AS3(也没有duplicateMovieClip()
)中一样,您必须实现自己的克隆方法。Java 确实有一个未实现的 '.clone()' 方法作为每个 Java 对象的一部分,因此如果您想要克隆一个特定的 View,您可以通过覆盖 clone 方法来实现您的克隆。
我认为您最终可能会做一些更类似于从库中实例化的事情,方法是在 xml 中制作小视图布局并使用 Inflater 工具对它们进行充气。
View result = null;
// where pContext is a context object, either supplied by the application
// or just by the current Activity (if available)
LayoutInflater inflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// where id is the layout id such as R.layout.myclonableview.
// where pRoot is the parent container for the new result.
// where pAttachToRoot is whether to immediately inflate the new view into the root.
result = inflater.inflate(id, pRoot, pAttachToRoot);
// Now "clone" your old view by copying relevant fields from the old one to the
// one stored in result