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

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

看代码:

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. (九)Knockout 进一步技术

    加载和保存 JSON 数据 Knockout允许您实现复杂的客户端交互,但是几乎所有web应用程序还需要与服务器交换数据,或者至少要序列化数据以供本地存储.交换或存储数据最方便的方式是JSON格式-- ...

  2. c# winform访问 带有windows身份验证的webservice

    1 将webservice设置为windows身份验证iis10中,要确认已安装windows身份验证在 控制面板 - >打开或关闭Windows功能 - >万维网服务 - >安全性 ...

  3. openresty开发系列31--openresty执行流程

    openresty开发系列31--openresty执行流程 我们先看个例子 location /test {    set $a 32;    echo $a;    set $a 56;    e ...

  4. Win10访问共享文件夹如何取消用户名密码

    win10中,开启guest(来宾)账户的步骤是: 右击win10左下角Windows的图标->计算机管理->计算机管理(本地)->系统工具->本地用户和组->用户-&g ...

  5. QT中常用工具总结

    1.qmake 利用.pro文件生成Makefile 命令为: eg: qmake -o Makefile hello.pro 2. uic 利用ui界面审查.h头文件 命令为: eg: uic go ...

  6. NI MAX中缺少串口(转)

    Software Measurement & Automation Explorer (MAX) Driver NI-VISA     问题详述 在NI MAX中,设备和接口中的串口不可用或缺 ...

  7. Pytest单元测试框架-测试用例运行规则

    1.Pytest测试用例运行规则 在pytest单元测试框架下面执行用例,需要满足以下几个特点: 1. 文件名以test_*.py开头或者*_test.py 2. 测试类.测试函数以test开头 3. ...

  8. [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

    原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...

  9. [LeetCode] 358. Rearrange String k Distance Apart 按距离k间隔重排字符串

    Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...

  10. [LeetCode] 741. Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...