简体   繁体   English

如何在现有按钮和按钮之间添加其他LinearLayout?

[英]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? 通过单击按钮,是否可以在现有按钮和按钮之间显示新的LinearLayout 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: 在您的代码中,您只需要在onClick方法中添加以下代码:

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. 使用这段代码,您将创建一个新的params布局,并向该布局中添加一个规则,在该示例中,该规则被编码为位于另一个元素下方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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