嘿,我将在下面的代码中简要描述我所做的事情:我创建一个 ViewStub (Vs) 并将其添加到 RelativeLayout (Rl)。然后我想在同一个 RL 中添加一个新的 Vs,所以我创建了 stub2 并将其添加到 rl。然后我参考第一个和第二个 Vs (rlInf1 ,rlInf2) 的 Rl (它是膨胀的)然后我设置 Rl.Layoutparams 以便 rlInf2 应该低于 rlInf1 但它们是重叠的。如何设置 LayoutParams 以使新的 ViewStub 低于旧的?有任何想法吗?
(我没有在 XML 中添加 ViewStub,而是在 Java 代码中添加)
//Löschen des Standard-Tv
TextView tv = (TextView) findViewById(R.id.tvNoContentInfo);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.Layout_ContentInfo);
rl.removeView(tv);
//Erstellen eines ViewStubs
ViewStub stub1 = new ViewStub(this);
//zum richtigen Layout hinzufügen
rl.addView(stub1);
stub1.setLayoutResource(R.layout.routen_navi_infocontent);
//Referenz zum eingefügten Layout herstellen
View inflated = stub1.inflate();
//Zugriff auf Inhalt des zufor referenzierten Layouts
TextView myTextView = (TextView) inflated.findViewById(R.id.tvHeaderInfocontent);
myTextView.setText("hey");
stub1.setId(View.generateViewId());
stub1.setInflatedId(stub1.getId());
RelativeLayout rlInf1 = (RelativeLayout) inflated.findViewById(R.id.Layout_infocontent);
ViewStub stub2 = new ViewStub(this);
//zum richtigen Layout hinzufügen
rl.addView(stub2);
stub2.setLayoutResource(R.layout.routen_navi_infocontent);
//Referenz zum eingefügten Layout herstellen
View inflated2 = stub2.inflate();
RelativeLayout rlInf2 = (RelativeLayout) inflated2.findViewById(R.id.Layout_infocontent);
RelativeLayout.LayoutParams p= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW,rlInf1.getId());
rlInf2.setLayoutParams(p);