简体   繁体   English

如何在两个元素之间添加一个元素

[英]How to add an element between two elements android

I am trying to add an editText in between two already existing editTexts programmatically on the click of a button in android. 我试图在android中单击button以编程方式在两个已经存在的editTexts之间添加一个editText I am just wondering if this is possible, as i have been unable to find any related questions? 我只是想知道这是否可能,因为我一直找不到任何相关问题?

you can add 3rd EditText on 2nd position. 您可以在第2个位置添加第3个EditText。

For that first you should have reference of the parent layout nad then do like this. 为此,您应该先参考父级布局,然后再这样做。

if you have done : 如果您已完成:

parent.addView(editText1);
parent.addView(editText2);

So now your parent have two child views. 因此,现在您的父母有两个孩子视图。

now to add 3rd EditText ie editText3 then do this like: 现在添加第三个EditText, editText3然后执行以下操作:

parent.addView(editText3, 1);// addView(<childview>, <index>);

Like this your 3rd EditText will be in 2nd position. 这样,您的第3个EditText将位于第2个位置。

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)yourEditBox.getLayoutParams();
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
params.addRule(RelativeLayout.RIGHT_OF, R.id.id_to_be_right_of);

yourEditBox.setLayoutParams(params);

Use above code to add and align you're editbox between another two editboxes. 使用上面的代码在两个编辑框之间添加并对齐您的编辑框。

Inside Activity class 内部活动课

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_main, null);
    setContentView(linearLayout);
    EditText editText = new EditText(getBaseContext());
    editText.setHint("Programmatically Added EditText");
    linearLayout.addView(editText, 1);
}}

Layout file structure 布局文件结构

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="EditText 1" />

   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="EditText 2" />

</LinearLayout>

Hope this helps. 希望这可以帮助。 Cheers! 干杯!

You can just add this editText between the two in your xml file and controle visibility on your xml and the moment you click on your button. 您只需在xml文件中的两者之间添加此editText,并控制xml的可见性以及单击按钮的那一刻。

In xml file :Set visibility to gone or invisible depending on what you actually want : android:visibility="invisible" it won't be visible but it's going to take place in your view android:visibility="gone" it won't be visible and it's not taking place in your view 在xml文件中:根据您的实际需要将可见性设置为不可见或不可见: android:visibility =“ invisible”它将不可见,但将在您的视图中发生android:visibility =“ gone”它将不可见可见并且在您看来它没有发生

In your code : 在您的代码中:

yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            yourButton.setVisibility(View.VISIBLE);

}); });

What do you want to acomplish with runtime element adding? 您想通过添加运行时元素来完成什么? Whats the purpose? 目的是什么? Maybe isn't enough to show/hide the element on specific actions? 也许不足以显示/隐藏特定动作上的元素?

I mean you can make it gone (it will be invisible but also won't use space on the layout) in xml: 我的意思是您可以在xml中使其消失(它将不可见,但也不会在布局上使用空间):

android:visibility="gone"

or in java code in the onCreate() method: 或在onCreate()方法的Java代码中:

specificElement.setVisibility(View.GONE)

Then when you normally would add the element you rather just set the visibility to visible: 然后,通常在添加元素时,您只需将可见性设置为visible:

specificElement.setVisibility(View.VISIBLE)

What about that? 那个怎么样?

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

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