以编程方式将ViewStub添加到RelativeLayout,而不是在第一个

时间:2016-08-11 12:15:13

标签: android android-relativelayout layoutparams viewstub

嘿,我将在下面的代码中简要介绍一下我的操作: 我创建一个ViewStub(Vs)并将其添加到RelativeLayout(R1)。 然后我想在同一个RL中添加一个新的V,所以我创建了stub2并将其添加到rl。 然后我引用第一个和第二个Vs(rlInf1,rlInf2)的R1(它是inflatet) 然后我设置Rl.Layoutparams,以便rlInf2应该低于rlInf1,但它们是重叠的。 如何设置LayoutParams以使新ViewStub低于旧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);

1 个答案:

答案 0 :(得分:1)

将视图扩展到ViewStub后,ViewStub消失了,因此更改其中的ID毫无用处。您需要在膨胀的视图上更改它。 所以改变

stub1.setId(View.generateViewId());

inflated.setId(View.generateViewId());

并将最后两行更改为:

    p.addRule(RelativeLayout.BELOW,inflated.getId());
    inflated2.setLayoutParams(p);

此外,您可能希望从LayoutParams视图中抓取现有的inflated而不创建新的ViewStubs,否则您将最终覆盖所有现有的。{/ p> 顺便说一句:我必须假设增加ViewStub和即时通货膨胀只是对某事而不是实际流动的考验,对吧? : - )

此外: 是的,您可以在不使用 View inflated = LayoutInflater.from(this).inflate(R.layout.routen_navi_infocontent, rl, false); inflated.setId(View.generateViewId()); rl.addView(inflated); View inflated2 = LayoutInflater.from(this).inflate(R.layout.routen_navi_infocontent, rl, false); RelativeLayout.LayoutParams p= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); p.addRule(RelativeLayout.BELOW,inflated.getId()); inflated2.setLayoutParams(p); rl.addView(inflated2); TextView myTextView = (TextView) inflated.findViewById(R.id.tvHeaderInfocontent); myTextView.setText("hey");

的情况下添加布局
var text = "I'm very upset, very distress, very agitated, very annoyed and very pissed";
var regex = /very/;
var synonyms = ["extremely", "exceedingly", "exceptionally", "especially", "tremendously"];