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

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

看代码:

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. 转 mysql 备份导致 waiting for global read lock

    ######转 https://blog.csdn.net/weixin_34038652/article/details/92129498 近业务高峰期间经常会有开发跳起来说应用连接数据库超时了! ...

  2. [LeetCode] 6. ZigZag Converesion 之字型转换字符串

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  3. 【SSH进阶之路】Spring的IOC逐层深入——Spring的IOC原理[通俗解释一下](三)

    1. IoC理论的背景我们都知道,在采用面向对象方法设计的软件系统中,它的底层实现都是由N个对象组成的,所有的对象通过彼此的合作,最终实现系统的业务逻辑. 图1:软件系统中耦合的对象 如果我们打开机械 ...

  4. ou can mix require and export. You can't mix import and module.exports.

    ou can mix require and export. You can't mix import and module.exports.

  5. 从Asp .net到Asp core (第二篇)《Asp Core 的生命周期》

    前面一篇文章简单回顾了Asp .net的生命周期,也简单提到了Asp .net与Asp Core 的区别,我们说Asp Core不在使用Asp.netRuntime,所以它也没有了web程序生命周期中 ...

  6. 数组中重复的数字(Python)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019-08-13 22:35 # @Author : daryl # @File : ...

  7. PowerBuilder中pbm_keydown()和pbm_dwnkey()的区别:

    原地址:https://vcoo.cc/blog/463/ PowerBuilder开发中我们经常会用到快捷键的事件编程,在PB中的键盘事件主要用三个:pbm_dwnkey.pbm_keydown.p ...

  8. 网页中插入Flash动画(.swf)代码和常用参数设置

    我们现在大部分人做网页,都是直接用DW插入flash,而且DW也是所见即所得,直接生成了相应的flash显示代码.可是我们又有多少人了解这些直接由DW生成的代码呢?其实我接触flash player标 ...

  9. [LOJ2002] [SDOI2017] 序列计数

    题目链接 LOJ:https://loj.ac/problem/2002 洛谷:https://www.luogu.org/problemnew/show/P3702 Solution 考虑补集转换, ...

  10. 【SQL Server数据迁移】64位的机器:SQL Server中查询ORACLE的数据

    从SQL Server中查询ORACLE中的数据,可以在SQL Server中创建到ORACLE的链接服务器来实现的,但是根据32位 .64位的机器和软件, 需要用不同的驱动程序来实现. 在64位的机 ...