以编程方式添加相对于另一个图像视图的图像视图(Android)

以编程方式添加相对于另一个图像视图的图像视图(Android),android,imageview,android-relativelayout,android-imageview,Android,Imageview,Android Relativelayout,Android Imageview,我试图以编程方式添加一个ImageView,然后添加另一个与此ImageView相关的ImageView。我的代码: RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.myLayout); ImageView anchor = new ImageView(getApplicationContext()); anchor.setImageDrawable(getResources().getDrawable(R.drawable

我试图以编程方式添加一个ImageView,然后添加另一个与此ImageView相关的ImageView。我的代码:

RelativeLayout mLayout = (RelativeLayout) findViewById(R.id.myLayout);
ImageView anchor = new ImageView(getApplicationContext());
anchor.setImageDrawable(getResources().getDrawable(R.drawable.anchor));
anchor.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams anchorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
anchorParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
anchorParams.addRule(RelativeLayout.CENTER_VERTICAL);   
mLayout.addView(anchor,anchorParams);
ImageView spinner = new ImageView(getApplicationContext());
spinner.setImageDrawable(getResources().getDrawable(R.drawable.image1));
spinner.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams spinnerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
spinnerParams.addRule(RelativeLayout.BELOW,anchor.getId());
mLayout.addView(spinner,spinnerParams);
第一个图像正好在我想要的地方-集中-但是第二个图像没有显示在锚的下方,而是显示在屏幕的顶部,状态栏的下方


我错过了什么吗?

我想,
anchor.getId()
的值为空。为锚点视图设置一个Id,在
addRule()

之前,我想,
anchor.getId()
会得到null。在使用anchor.getId()和anchor.setId(int)之前,我认为您需要以编程方式为anchor视图设置一个Id。

我认为在使用anchor.getId()和anchor.setId(int)之前,您需要以编程方式为anchor视图指定一个Id。

很难说什么是错的,尝试生成XML并添加它

LinearLayout myLayout = (LinearLayout) findViewById(R.id.LogoView);

                View hiddenInfo = getLayoutInflater().inflate(R.layout.search,
                        myLayout, false);
                myLayout.removeAllViews();
                myLayout.addView(hiddenInfo);

很难说什么是错的,试着生成XML并添加它

LinearLayout myLayout = (LinearLayout) findViewById(R.id.LogoView);

                View hiddenInfo = getLayoutInflater().inflate(R.layout.search,
                        myLayout, false);
                myLayout.removeAllViews();
                myLayout.addView(hiddenInfo);