我正在尝试以编程方式添加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);
第一张图片就在我想要的地方 - 集中 - 但第二张图片没有显示在锚点下方,而是出现在屏幕顶部,就在状态栏下方。
我错过了什么吗?
答案 0 :(得分:3)
我认为,anchor.getId()
为空。在addRule()
答案 1 :(得分:1)
我相信您需要以编程方式为锚定一个id,然后才能使用anchor.setId(int)来使用anchor.getId()。
答案 2 :(得分:0)
很难说出了什么问题,尝试制作XML并添加它
LinearLayout myLayout = (LinearLayout) findViewById(R.id.LogoView);
View hiddenInfo = getLayoutInflater().inflate(R.layout.search,
myLayout, false);
myLayout.removeAllViews();
myLayout.addView(hiddenInfo);