简体   繁体   中英

How can I add an additional LinearLayout between an existing one and a button?

Is it possible to show a new LinearLayout between an existing one and a button by clicking that button? I want to show a new field to be complete.

This picture illustrates what I mean:

显示我想在哪里添加新的LinearLayout

In the world of programming, anything is possible (I've been told by many developers).
In your code, you simply need to add the following code in the onClick method:

LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setBackgroundColor(Color.parseColor("#135517"));

Setting background is just an example of what it's capable of achieving. One thing I would recommend you to do is to position it properly to ensure it fits the hierarchy accordingly.
For example:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewToLayout.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.below_id);

With this piece of code, you are creating a new params layout and add a rule to the layout which in this example is coded to position below another element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM