我可以重复使用LayoutPrams与ViewGroup.addView? [英] May I reuse LayoutPrams with ViewGroup.addView?

查看:267
本文介绍了我可以重复使用LayoutPrams与ViewGroup.addView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实 ViewGroup.addView 克隆的LayoutParams 数据转换成内部或链接呢?我可以重复使用的<​​code>的LayoutParams 多次调用同一个实例 addView()有不同的看法?

有一个在apidoc一无所知。

WOW

答案是否定的(检查实验):

 公共类SymbolPadActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){

    super.onCreate(savedInstanceState);

    RelativeLayout.LayoutParams labelParams;

    / *
     *此块重用不工作
    labelParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
    labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    * /


    RelativeLayout的先行者=新RelativeLayout的(这一点);

    的TextView TextView的;
    对于(INT LEFTMARGIN = 0; LEFTMARGIN&LT; 3000; LEFTMARGIN + = 100){
        对于(INT TOPMARGIN = 0; TOPMARGIN&LT; 800; TOPMARGIN + = 40){

            //我不能忽略这3行
            labelParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
            labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);

            labelParams.leftMargin = LEFTMARGIN;
            labelParams.topMargin = TOPMARGIN;

            TextView的=新的TextView(本);
            textView.setText((+ LEFTMARGIN +,+ TOPMARGIN +));
            mover.addView(TextView的,labelParams);

        }
    }




    RelativeLayout.LayoutParams moverParams =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    moverParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
    moverParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    moverParams.leftMargin = 0;
    moverParams.topMargin = 0;

    RelativeLayout的定子=新RelativeLayout的(这一点);
    stator.addView(先行者,0,moverParams);

    的setContentView(定子);


}
 

}

解决方案
  

有一个在apidoc一无所知。

这意味着你需要做较为保守的选择,无论是目前的实现是什么,作为实施可能会改变。

因此​​,你需要假设它不保存重复使用的<​​code>的LayoutParams 的实例与不同的视图使用

FWIW,AFAICT,这是真的呢。 - 的ViewGroup 不会出现,使复印件

Does ViewGroup.addView clones LayoutParams data into inside or links to it? May I reuse the same instance of LayoutParams with multiple calls to addView() with different views?

There is nothing about it in apidoc.

WOW

The answer is NO (checked experimentally):

public class SymbolPadActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    RelativeLayout.LayoutParams labelParams;

    /*
     * This block to reuse is not working
    labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    */


    RelativeLayout mover = new RelativeLayout(this);

    TextView textView;
    for(int leftMargin = 0; leftMargin<3000; leftMargin += 100) {
        for(int topMargin=0; topMargin<800; topMargin += 40) {

            // I can't omit these 3 lines 
            labelParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            labelParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            labelParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

            labelParams.leftMargin = leftMargin;
            labelParams.topMargin = topMargin;

            textView = new TextView(this);
            textView.setText("(" + leftMargin + "," + topMargin + ")");
            mover.addView(textView, labelParams);

        }
    }




    RelativeLayout.LayoutParams moverParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    moverParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    moverParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    moverParams.leftMargin = 0;
    moverParams.topMargin = 0;

    RelativeLayout stator = new RelativeLayout(this);
    stator.addView(mover, 0, moverParams);

    setContentView(stator);


}

}

解决方案

There is nothing about it in apidoc.

This means you need to make the more conservative choice, no matter what the current implementation is, as the implementation could change.

Hence, you need to assume that it is not save to reuse an instance of LayoutParams for use with different Views.

FWIW, AFAICT, that is true anyway -- ViewGroup does not appear to make a copy.

这篇关于我可以重复使用LayoutPrams与ViewGroup.addView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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