Android 动态添加移除控件表单linearlayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10515589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:01:25  来源:igfitidea点击:

dynamically add remove control form linearlayout

androidandroid-linearlayout

提问by AMH

I have 3 layouts, I need when click on button access certain layout and ad remove controls from and in it any idea how to achieve that , this is the code I use

我有 3 个布局,当点击按钮访问某些布局和广告时,我需要从中删除控件,并且知道如何实现它,这是我使用的代码

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

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.20"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/backbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Back" />

        <Button
            android:id="@+id/backbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="next" />
    </LinearLayout>
    <!-- the two columns part -->

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.80"
        android:orientation="horizontal" >

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.80" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="First Name" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="0.20" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="second Name" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

回答by K_Anas

Remove view from parent on Android:

从 Android 上的父级移除视图:

View myView = findViewById(R.id.my_view);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);

Android remove all child views:

Android 删除所有子视图:

LinearLayout formLayout = (LinearLayout)findViewById(R.id.formLayout);
formLayout.removeAllViews();

Add view to parent on Android:

在 Android 上将视图添加到父级:

Button myButton = new Button(getApplicationContext());
myButton.setLayoutParameters(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT,
                                     LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

you can use:

您可以使用:

LinearLayout.LayoutParams.FILL_PARENT

or

或者

LinearLayout.LayoutParams.WRAP_CONTENT

回答by kameny

Add andtheitroadd:id="@+id/linerlayout_1"after that you can access this view easy inside the code with the findviewbyid()method.

添加andtheitroadd:id="@+id/linerlayout_1"之后,您可以使用该findviewbyid()方法在代码中轻松访问此视图。

For examlpe: place this inside the button's onclicklistener method.

例如:将它放在按钮的 onclicklistener 方法中。

LinearLayout ll = (LinearLayout) findViewById(R.id.linerlayout);
ll.setVisibility(View.GONE); // this row will hide the entire linerlayout
ll.addView(someView); //this row will add the specified View f.e.: TextView
ll.removeView(otherView); // this row will remove the view

And you can manage th view visibility by xml attribute android:visibilitytoo.

您也可以通过 xml 属性管理视图可见性android:visibility

回答by Ika

Look at the setVisibility method of the View class. It allows you to make Views appear or disappear from the UI very simply.

查看 View 类的 setVisibility 方法。它允许您非常简单地使视图从 UI 中出现或消失。

In your button's click listener just add view.setVisibility(View.GONE)to make any layout or widget go away. You can make it reappear by calling view.setVisibility(View.VISIBLE)

在按钮的单击侦听器中,只需添加view.setVisibility(View.GONE)即可使任何布局或小部件消失。您可以通过调用使其重新出现view.setVisibility(View.VISIBLE)

回答by Bobs

Sample for dynamically add or remove a view:

动态添加或删除视图的示例:

TextView tv = new TextView(this);

        tv.setWidth(LayoutParams.WRAP_CONTENT);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        params.gravity = Gravity.RIGHT;
        tv.setLayoutParams(params);
        tv.setTextAppearance(this, R.style.darkTextNormal);
        tv.setBackgroundResource(R.color.lightBlue);
        tv.setTextSize(16);

yourLinearLayout.addView(tv);

// or

// 或者

yourLinearLayout.removeView(tv);