我可以将ViewPrams重用于ViewGroup.addView吗?

Suz*_*ioc 10 android android-layout

是否ViewGroup.addView克隆LayoutParams数据到内部或链接呢?我是否可以重复使用具有不同视图的LayoutParams多个调用的相同实例addView()

在apidoc中没有任何关于它的内容.

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

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);


}
Run Code Online (Sandbox Code Playgroud)

}

Com*_*are 5

在apidoc中没有任何关于它的内容.

这意味着无论当前实现是什么,您都需要做出更保守的选择,因为实现可能会发生变化.

因此,您需要假设不重复LayoutParams使用不同的实例Views.

至于它的价值,据我所知,无论如何这是真的 - ViewGroup似乎没有复制品.