我正在尝试复制relativeLayout.LayoutParams,并且我需要新的Params来完全像旧的一样.因此,请考虑以下方法:
private relativeLayout.LayoutParams copyParams(relativeLayout.LayoutParams source){ relativeLayout.LayoutParams copy = new relativeLayout.LayoutParams((relativeLayout.LayoutParams)source); return copy;}
现在,这可以正常工作,但是只能在Eclipse 19以上的API级别19以上编译.
为了使此工作在API-19之前完成,我尝试了以下 *** 作:
private relativeLayout.LayoutParams copyParams(relativeLayout.LayoutParams source){ relativeLayout.LayoutParams copy = new relativeLayout.LayoutParams((VIEwGroup.LayoutParams)source); copy.setmargins(source.leftmargin, source.topmargin, source.rightmargin, source.bottommargin); return copy;}
通过强制转换为VIEwGroup.LayoutParams,我可以强制使用VIEwGroup.LayoutParams copyConstructor.它很容易获得,但是只会复制高度和宽度.因此,我需要自己转移边距,这在示例2的第3行中进行.但是,这仍然在源布局中遗漏了规则,例如relativeLayout.BELOW.
我在文档的任何地方都找不到copyRules()或setRules()方法(或任何等效方法),但是我可以看到relativeLayout.LayoutParams的addRule()方法.考虑到source.getRules()返回一个数组,我认为我可以循环遍历并将每个规则添加到我的新LayoutParams中,但这感觉像是我做错了.有没有复制这些规则的明智方法?
TL; DR:在API 19之前,有没有一种方法可以复制批发的relativeLayout.LayoutParams规则,如果没有,那么复制它们的最疯狂的方法是什么?
解决方法:
这是我复制带有所有规则的relativeLayout.LayoutParams的 *** 作:
@NonNullprivate static relativeLayout.LayoutParams copy(VIEwGroup.LayoutParams vIEwLayoutParamsTocopy) { relativeLayout.LayoutParams copIEdParams = new relativeLayout.LayoutParams(vIEwLayoutParamsTocopy); if (vIEwLayoutParamsTocopy instanceof relativeLayout.LayoutParams) { relativeLayout.LayoutParams relativeLayoutParamsTocopy = (relativeLayout.LayoutParams) vIEwLayoutParamsTocopy; int[] rulesTocopy = relativeLayoutParamsTocopy.getRules(); for (int verb = 0; verb < rulesTocopy.length; verb++) { int subject = rulesTocopy[verb]; copIEdParams.addRule(verb, subject); } } return copIEdParams;}
为了使其工作,我必须仔细阅读getRules()方法的文档:
总结RetrIEves a complete List of all supported rules, where the index is the rule verb, and the element value is the value specifIEd, or “false” if it was never set.
以上是内存溢出为你收集整理的android-复制API 19之前的RelativeLayout.LayoutParams规则全部内容,希望文章能够帮你解决android-复制API 19之前的RelativeLayout.LayoutParams规则所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)