动态添加删除控制形式的LinearLayout [英] dynamically add remove control form linearlayout

查看:553
本文介绍了动态添加删除控制形式的LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个版面,我需要的时候,点击按钮获取一定的布局和广告的,并在其中任何想法如何做到这一点移除控件,这是code我用

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>

    <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0.20
        机器人:方向=横向>

        <按钮
            机器人:ID =@ + ID /后退按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=后退/>

        <按钮
            机器人:ID =@ + ID /后退按钮
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=下一个/>
    < / LinearLayout中>
    <! - 两列的部分 - >

    <的LinearLayout
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:layout_weight =0.80
        机器人:方向=横向>

        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =0.80>

            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=名/>
        < / LinearLayout中>

        <的LinearLayout
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:layout_weight =0.20>

            <的TextView
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文本=第二名称/>
        < / LinearLayout中>
    < / LinearLayout中>

< / LinearLayout中>
 

解决方案

删除视图从父在Android上:

 查看MyView的= findViewById(R.id.my_view);
ViewGroup中父=(ViewGroup中)myView.getParent();
parent.removeView(MyView的);
 

Android的删除所有子视图:

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

添加以母公司在Android:

 按钮myButton的=新的按钮(getApplicationContext());
myButton.setLayoutParameters(新LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT,
                                     LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton的);
 

您可以使用:

  LinearLayout.LayoutParams.FILL_PARENT
 

  LinearLayout.LayoutParams.WRAP_CONTENT
 

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

<?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>

解决方案

Remove view from parent on Android:

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

Android remove all child views:

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

Add view to parent on 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

这篇关于动态添加删除控制形式的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆