我对如何在RelativeLayout中创建视图(以编程方式)感到困惑。目前我正在使用.layout来定位RelativeLayout中的视图(在java中创建),但是如果我尝试使用LayoutParams添加视图来定位视图&不使用.layout,这些视图不会添加到relativelayout。那是为什么?
public class MyCustomRelativeLayout extends RelativeLayout
{
public void addViews(){
MyCustomImageView myImage = new MyCustomImageView(context);
myImage.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
myImage.setImageResource(R.drawable.ic_launcher);
addView(myImage); // this one isn't visible/added in RelativeLayout.
ImageView image = new ImageView(context);
image.layout(0,0,300,400);
image.setImageResource(R.drawable.garden);
addView(image); // this one is visible/added in Relativelayout
}
}
public class MyCustomImageView extends ViewGroup
{
public void onLayout(int l, int t, int r, int b)
{
this.layout(l,t,r,b);
}
public void addImageViews()
{
ImageView mImage = new ImageView(context);
mImage.layout(0,0,200,200);
mImage.setImageResource(R.drawable.hello);
addView(mImage); // this one is visible/added in Relativelayout
ImageView mImage2 = new ImageView(context);
mImage2.layout(0,0,70,70);
mImage2.setImageResource(R.drawable.hello2);
addView(mImage2); // this one is visible/added in Relativelayout
}
}
为什么会这样?
And When to use .layout instead of LayoutParams?
最好的问候
答案 0 :(得分:1)
LayoutParams dosn不会对视图进行充气。它只是向父母描述了他如何使观点膨胀。