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

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

看代码:

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. 《精通CSS第3版》(5)漂亮的盒子

  2. 解决electron-vue中无法Element的Tooltip组件

    打开文件:electron-vue/webpack.renderer.config.js 在大约21行左右找到 let whiteListedModules 将element-ui添加进去,最终如下所 ...

  3. CEF CefSettings 结构体 详解

    1. single_process: 设置为ture时,browser和render使用同一个进程.Chromium 不正是支持此运行模式,并且不如默认的多进程稳定. 2. no_sandbox: 沙 ...

  4. Nginx 配置 stream SSL 第四层 代理

    场景:服务器F针对访问终端需要添加白名单操作,由到终端数量较多,所以用了一台代理服务器 P,在服务F中添加 服务器P IP地址的白名单,所有终端访问服务器P 由于我已经安装过 Nginx 所以只需要添 ...

  5. Jmeter-Critical Section Controller(临界区控制器)(还没看,是一个控制请求按顺序执行的东东)

    The Critical Section Controller ensures that its children elements (samplers/controllers, etc.) will ...

  6. Laya页面嵌套和Scene.destory导致的Bug

    Laya2.1.1.1 参考: 预设使用 Laya给出了相同模块,逻辑代码也相同情况下,使用页面嵌+runtime的使用方案.但是该方案和Laya.Scene.open,Laya.Scene.dest ...

  7. python-机器学习-深度学习-算法-面试题

    GitHub 地址: https://github.com/taizilongxu/interview_python https://github.com/imhuay/Algorithm_Inter ...

  8. jxls:用jx:if实现字典值格式化

    用JXLS导出excel非常方便,但是我们往往需要把字典值转为中文名称,例如1转为男,这个时候就需要用到jx:if来完成. 请见下图: jx:if(condition="obj.way==1 ...

  9. icheck判断是否选中

    icheck判断是否选中   1 $("#id").on('ifChanged', function () { 2 if ($(this).is(':checked')) {//就 ...

  10. abp_vue导入导出excel

    后端abp,前端vue导入excel,开始准备用直接用npoi,觉得要写太多的代码,就算从以前的复制粘贴也麻烦,所以偷懒直接用别人的轮子 Magicodes.IE.这样可以节省很多工作,根据实体生成e ...