0

嘿,我将在下面的代码中简要描述我所做的事情:我创建一个 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);
4

1 回答 1

1

一旦将视图膨胀到 中ViewStubViewStub就消失了,因此更改其上的 id 是没有用的。您需要在膨胀视图上更改它。所以改变

stub1.setId(View.generateViewId());

inflated.setId(View.generateViewId());

并将最后两行更改为:

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

此外,您可能希望LayoutParams从视图中获取现有inflated视图而不是创建新视图,否则您最终将覆盖所有现有视图。

顺便说一句:我不得不假设增加ViewStubs即时通胀只是对某些东西的测试,而不是真正的流量,对吧?:-)

添加:是的,您可以在不使用的情况下添加布局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");
于 2016-08-11T13:08:37.207 回答