想要一个功能,点击按钮,可以在已有的布局上,新添加一组组件。

动态的创建组件,本质上还是创建组件,只不过是在程序中根据逻辑来创建。大致步骤是找到要创建控件的位置,然后将要创建的组件添加进去。

看代码:

MainActivity.java

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.bt);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.widgets); Button bt = new Button(getApplicationContext());
bt.setText("BUTTON"); TextView tv = new TextView(getApplicationContext());
tv.setText("TEXTVIEW"); mLinearLayout.addView(bt);
mLinearLayout.addView(tv);
}
});
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我添加" /> <ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <LinearLayout
android:id="@+id/widgets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView> </LinearLayout>

运行结果:

LayoutInflater

LayoutInflater作用了findViewById()作用类似,不同的是LayoutInflater是用来找res/layout/下的xml文件,并且实例化,而findViewById是找xml文件中的控件等。

对于一个没有被载入或者降妖动态载入的界面,都需要LayoutInflater.inflate来载入

对于一个已经载入的界面,使用findViewById来获取其中的元素

修改一下上面的代码:

MainActivity.java

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.bt);
button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.widgets);
// com.example.test.MyLinearLayout myLinearLayout = new
// com.example.test.MyLinearLayout(
// getApplicationContext());
// mLinearLayout.addView(myLinearLayout); LayoutInflater.from(getApplicationContext()).inflate(R.layout.widget, mLinearLayout); }
});
} }

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我添加" /> <ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content" > <LinearLayout
android:id="@+id/widgets"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="~~~~~~~~~~~" />
</LinearLayout>
</ScrollView> </LinearLayout>

MyLinearLayout.java

package com.example.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.LinearLayout; public class MyLinearLayout extends LinearLayout { private Context mContext; public MyLinearLayout(Context context) {
super(context);
mContext = context; LayoutInflater.from(context).inflate(R.layout.widget, this);
} }

widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tttt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="111" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="222" /> </LinearLayout>

运行结果

Android笔记(六十一)动态添加组件的更多相关文章

  1. Angular使用总结 --- 通过指令动态添加组件

    之前自己写的公共组件,都是会先引入,需要调起的时候再通过service控制公共组件状态.值.回调函数什么的.但是有一些场景不适合这种方式,还是动态添加组件更加好.通过写过的一个小组件来总结下. 创建组 ...

  2. easyui 动态添加组件 要重新渲染

    做项目时动态添加组件是常有的事,easyui动态添加组件时样式会失效,这是因为这个组件没有经过 easyui的解析器解析, 比如:   <pre name="code" cl ...

  3. 【Android初级】如何动态添加菜单项(附源码+避坑)

    我们平时在开发过程中,为了灵活多变,除了使用静态的菜单,还有动态添加菜单的需求.今天要分享的功能如下: 在界面的右上角有个更多选项,点开后,有两个子菜单:关于和退出 点击"关于", ...

  4. Android 在程序中动态添加 View 布局或控件

    有时我们需要在程序中动态添加布局或控件等,下面用程序来展示一下相应的方法: 1.addView 添加View到布局容器 2.removeView 在布局容器中删掉已有的View 3.LayoutPar ...

  5. 使用js动态添加组件

    在文章开始之前,我想说两点 1 自己初学js,文章的内容在大神看来可能就是不值一提,但是谁都是从hello world来的,望高   手不吝指教# 2 我知道这个标题起的比较蛋疼,大家看图就能说明问题 ...

  6. 动态添加组件(XML)

    1.利用LayoutInflater的inflate动态加载XMLmLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID);La ...

  7. vue2.0动态添加组件

    方法一.<template> <input type="text" v-model='componentName'> <button @click=' ...

  8. 【Android 开发教程】动态添加Fragments

    本章节翻译自<Beginning-Android-4-Application-Development>,如有翻译不当的地方,敬请指出. 原书购买地址http://www.amazon.co ...

  9. Android笔记(六十九) 仿微信界面(一)

          综合之前的Fragment和自定义组件的知识,实现微信界面 MainActivity.java package cn.lixyz.test; import android.app.Acti ...

随机推荐

  1. 10分钟完成一个最最简单的BLE蓝牙接收数据的DEMO

    这两天在研究蓝牙,网上有关蓝牙的内容非常有限,Github上的蓝牙框架也很少很复杂,为此我特地写了一个最最简单的DEMO,实现BLE蓝牙接收数据的问题, 不需要什么特定的UUID, 不需要什么断开重连 ...

  2. 嵌入式LINUX基础教程 第2版

    嵌入式LINUX基础教程  第2版 目录 第1章 入门 11.1 为什么选择Linux 11.2 嵌入式Linux现状 21.3 开源和GPL 21.4 标准及相关组织 31.4.1 Linux标准基 ...

  3. Python下numpy的使用

    首先:当然是欢迎大家了! Numpy : NumPy系统是Python的一种开源的数值计算扩展.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structur ...

  4. 高级UI-画板Canvas

    Canvas可以用来绘制直线.点.几何图形.曲线.Bitmap.圆弧等等,做出很多很棒的效果,例如QQ的消息气泡就是使用Canvas画的 Canvas中常用的方法 初始化参数 Paint paint ...

  5. spring中Bean的懒加载

    在ApplicationContext容器中,当容器一启动时,所有的bean(单例的)都会被创建和注入依赖,这也被视为IOC容器启动过程中的一个步骤. 那如何让一个bean在需要的时候再被创建,而不是 ...

  6. Qt5 QtQuick系列----QtQuick的Secne Graph剖析(1)

    教是言词, 实不是道,道本无言, 言说是妄.------- 达摩 Qt 5提出了一个新的渲染底层,以替代Qt4时期的Graphics View,这个渲染底层就是Scene Graph.Scene Gr ...

  7. LeetCode 541. 反转字符串 II(Reverse String II)

    541. 反转字符串 II 541. Reverse String II

  8. Python解释器安装与环境变量添加

    Python解释器安装与环境变量添加 Python解释器安装(3.6和2.7): www.python.org这个是python解释器的官网,一定要牢记. 鉴于市场上有两种python版本(2和3), ...

  9. Makefile 介绍

    makefile:是告诉编译器(交叉工具链)如何去编译.链接一个工程的规则.   一.概述 什 么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为 ...

  10. rsync 使用

    rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件. rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分, ...