如何创建复制副本视图? [英] How to create Clone-Duplicate View?

查看:95
本文介绍了如何创建复制副本视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android应用程序中,我想创建已经创建的Imagebutton的副本ImageButton.

In my android application, I want to create duplicate ImageButton of already created Imagebutton.

我想以编程方式创建新的Imagebutton,其具有与XML文件中已创建的按钮相同的宽度,高度,背景,图像src,边距等.简而言之,我想创建重复的ImageButton.

I want to create new Imagebutton programmatically having same widht, height, background, image src, margins etc. of already created button in XML file. In short, I want to create duplicate ImageButton.

我尝试过

ImageButton mImageButton = (ImageButton) findViewById(R.id.ib);
Imagebutton duplicate = mImageButton;

但是它仅引用mImageButton.因此,duplicate中的更改也会导致mImageButton中的更改.

But it only refer to the the mImageButton. So, change in duplicate also cause change in mImageButton.

请帮帮我.谢谢...

Please help me out. Thank you...

推荐答案

您无法克隆视图,方法是每次都创建视图.

You cannot clone views, the way to do it is to create your View each time.

您总是可以从XML多次放大视图,也可以创建一个函数以编程方式创建视图.

You could always inflate the view multiple times from an XML or create a function to create the view programatically.

通货膨胀:

private void addImageButton(ViewGroup viewGroup) {    
    View v = LayoutInflater.from(this).inflate(R.layout.ib, null);
    viewGroup.addView(v);
}

以编程方式:

private void addImageButton(ViewGroup viewGroup) {    
    ImageButton imageButton = new ImageButton(context);
    viewGroup.addView(imageButton);
}

这篇关于如何创建复制副本视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆