我有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>

最佳答案

在Android上从父级删除 View :

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

Android删除所有 subview :
LinearLayout formLayout = (LinearLayout)findViewById(R.id.formLayout);
formLayout.removeAllViews();

在Android上将 View 添加到父级:
Button myButton = new Button(getApplicationContext());
myButton.setLayoutParameters(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT,
                                     LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

您可以使用:
LinearLayout.LayoutParams.FILL_PARENT

或者
LinearLayout.LayoutParams.WRAP_CONTENT

关于android - 动态添加删除控件表格linearlayout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10515589/

10-11 22:33
查看更多