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

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

看代码:

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. python限定方法参数类型、返回值类型、变量类型等

    typing模块的作用 自python3.5开始,PEP484为python引入了类型注解(type hints) 类型检查,防止运行时出现参数和返回值类型.变量类型不符合. 作为开发文档附加说明,方 ...

  2. [译]如何取消本地的git commit提交?

    git reset HEAD~1 原文来源:https://stackoverflow.com/questions/4850717/how-to-cancel-a-local-git-commit

  3. forever at your feet

    A locket on a chainA bow that's made from rainA briar grows entwined with roseI've come to be foreve ...

  4. Sql server 中将数据行转列列转行(二)

    老规矩,先弄一波测试数据,数据填充代码没有什么意义,先折叠起来: /* 第一步:创建临时表结构 */ CREATE TABLE #Student --创建临时表 ( StuName ), --学生名称 ...

  5. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 插入删除和获得随机数O(1)时间 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  7. 【SSH进阶之路】Spring的AOP逐层深入——采用注解完成AOP(七)

    上篇博文[SSH进阶之路]Spring的AOP逐层深入——AOP的基本原理(六),我们介绍了AOP的基本原理,以及5种通知的类型, AOP的两种配置方式:XML配置和Aspectj注解方式. 这篇我们 ...

  8. linux查看哪个进程占用磁盘IO

    方法一: $ iotop -oP 命令的含义:只显示有I/O行为的进程 测试结果: 方法二: $ pidstat -d 1 命令的含义:展示I/O统计,每秒更新一次 测试结果:

  9. IDEA下同时使用Git和svn

    使用Git时将文件改成Git,Svn时改成svn 修改项目下.idea目录的vcs.xml配置文件. <?xml version="1.0" encoding="U ...

  10. DB2执行计划分析

    多表连接的三种方式详解 hash join.merge join. nested loop 项目中的SQL执行效率太低,就用执行计划看一下执行SQL,看不懂,百度一下,纪录下来: 大多数人从来没有听说 ...