动态添加控件及将某XML动态加入到Activity显示
一、动态添加控件、设置参数
这个难度比较大,放在前面讲,用的也比较多,普通情况下,我们会提前把布局XML写好,然后对XML中的元素进行设置,但这种方法在有些情况下就显得不适合,比较聊天应用,比如帖子的回复情况。针对这些情况,我们要动态根据获取到的数据增加控件或控件组的数量,废话不多说,下面就开整吧,先看个效果图:
原始XML 动态添加控件后
所做的工作:
1、在原有的界面的基础上添加一个LinearLayout layout;参数设置为:layout_width:wrap_content;layout_height:wrap_content;
对应代码:
- LinearLayout layout = new LinearLayout(this); // 线性布局方式
- layout.setOrientation(LinearLayout.HORIZONTAL); //
- layout.setBackgroundColor(0xff00ffff);
- LinearLayout.LayoutParams LP_MM = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
- layout.setLayoutParams(LP_MM);
2、添加一个ImageView;参数设置成layout_width:50;layout_height:50;
- ImageView imageView= new ImageView(this);
- imageView.setBackgroundResource(R.drawable.maomao);
- LinearLayout.LayoutParams PARA = new LinearLayout.LayoutParams(50,50);
- imageView.setLayoutParams(PARA);
- layout.addView(imageView);
3、添加一个TextView;参数设置成layout_width:wrap_content;layout_height:wrap_content;
对应代码:
- TextView tv = new TextView(this); // 普通聊天对话
- tv.setText("我和猫猫是新添加的");
- tv.setBackgroundColor(Color.GRAY);
- LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- tv.setLayoutParams(LP_WW);
- layout.addView(tv);
4、获取当前布局,即当前main_activity的LinearLayout布局(这里有两种方法)
方法一:(这种方法不需要:setContentView(R.layout.activity_main);)
- // 获取需要被添加控件的Linear布局(方法一)
- final LayoutInflater inflater = LayoutInflater.from(this);
- LinearLayout lin = (LinearLayout) inflater.inflate(
- R.layout.activity_main, null).findViewById(
- R.id.mainLinearLayout);
方法二:
- // 获取需要被添加控件的Linear布局(方法二)
- setContentView(R.layout.activity_main);
- final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
5、将动态增加的布局添加到当前布局中并显示;
- lin.addView(layout);
- setContentView(lin);
5、添加对新增的ImageView的单击消息响应
- //向动态添加的imageView,添加点击响应
- imageView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
- }
- });
全部代码:
- package com.example.try_add_combination_ctrl;
- /**
- * 动态增加控件组
- * @author harvic
- * @date 2014-1-9
- */
- import android.os.Bundle;
- import android.app.Activity;
- import android.graphics.Color;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // // 获取需要被添加控件的Linear布局(方法一)
- // final LayoutInflater inflater = LayoutInflater.from(this);
- // LinearLayout lin = (LinearLayout) inflater.inflate(
- // R.layout.activity_main, null).findViewById(
- // R.id.mainLinearLayout);
- // 获取需要被添加控件的Linear布局(方法二)
- setContentView(R.layout.activity_main);
- final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
- // 添加一个LinearLayout布局,设置成layout_width:wrap_content;layout_height:wrap_content;
- LinearLayout layout = new LinearLayout(this); // 线性布局方式
- layout.setOrientation(LinearLayout.HORIZONTAL); //
- layout.setBackgroundColor(0xff00ffff);
- LinearLayout.LayoutParams LP_MM = new LinearLayout.LayoutParams(
- LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
- layout.setLayoutParams(LP_MM);
- //添加一个ImageView,设置成layout_width:50;layout_height:50;
- ImageView imageView = new ImageView(this);
- imageView.setBackgroundResource(R.drawable.maomao);
- LinearLayout.LayoutParams PARA = new LinearLayout.LayoutParams(50, 50);//
- imageView.setLayoutParams(PARA);
- layout.addView(imageView);
- //添加一个TextView,设置成layout_width:wrap_content;layout_height:wrap_content;
- TextView tv = new TextView(this); // 普通聊天对话
- tv.setText("我和猫猫是新添加的");
- tv.setBackgroundColor(Color.GRAY);
- LinearLayout.LayoutParams LP_WW = new LinearLayout.LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- tv.setLayoutParams(LP_WW);
- layout.addView(tv);
- //将动态增加的布局添加到当前布局中;
- lin.addView(layout);
- setContentView(lin);
- // 向动态添加的imageView,添加点击响应
- imageView.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT)
- .show();
- }
- });
- }
- }
源码在文章最底部给出
二、将某一XML动态加入到当前Activity显示
这里就跟上面的不一样了,上面的是动态生成的控件或控件组,但这里并不是动态生成的,只是将一个写好的XML在运行时加入到当前Activity的XML中显示;
先看XML布局吧
1、原来的XML(activity_main.xml)
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/mainLinearLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:background="#00ff00"
- android:text="我是原生的,下面的布局是添加的"
- android:textSize="16sp" />
- </LinearLayout>
2、要增加进去的XML(combination_ctrl.xml)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/combineCtrl"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <ImageView android:id="@+id/img"
- android:layout_width="100dip"
- android:layout_height="100dip"
- android:layout_margin="10.0dip"
- android:padding="2.0dip"
- android:scaleType="fitXY"/>
- <LinearLayout android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFFFFF00"
- android:textSize="22px" />
- <TextView android:id="@+id/info"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FF00FFFF"
- android:textSize="13px" />
- </LinearLayout>
- </LinearLayout>
看看效果:
原形状态 增加进去后
全部代码
- package com.example.try_add_layout_from_xml;
- /**
- * 将一个现有的XML代码加入到当前的XML中,但由于ID是一定的,所以与在代码中添加include效果一样
- * @author harvic
- * @date 2014-1-9
- */
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.widget.ImageView;
- 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);
- final LayoutInflater inflater = LayoutInflater.from(this);
- // 获取需要被添加控件的布局
- final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
- // 获取需要添加的布局
- LinearLayout layout = (LinearLayout) inflater.inflate(
- R.layout.combination_ctrl, null).findViewById(R.id.combineCtrl);
- // 将布局加入到当前布局中
- lin.addView(layout);
- ImageView imageView = (ImageView) findViewById(R.id.img);
- imageView.setBackgroundResource(R.drawable.maomao);
- TextView TV_info = (TextView) findViewById(R.id.info);
- TV_info.setText("第一个INOF");
- TextView TV_name = (TextView) findViewById(R.id.name);
- TV_name.setText("第一个NAME");
- }
- }
可见代码非常短,而且关键代码就只有五句,专门列出来
- setContentView(R.layout.activity_main);
- final LayoutInflater inflater = LayoutInflater.from(this);
- // 获取需要被添加控件的布局
- final LinearLayout lin = (LinearLayout) findViewById(R.id.mainLinearLayout);
- // 获取需要添加的布局
- LinearLayout layout = (LinearLayout) inflater.inflate(
- R.layout.combination_ctrl, null).findViewById(R.id.combineCtrl);
- // 将布局加入到当前布局中
- lin.addView(layout);
源码在文章最底部
三、相关代码设置参数汇总
1、设置margin
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- lp.setMargins(10, 20, 30, 40);
- imageView.setLayoutParams(lp);
2、设置layout_weight:
- setLayoutParams(new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.FILL_PARENT,
- LinearLayout.LayoutParams.FILL_PARENT,weight
- ));
例如:
- TextView tv_like = new TextView(this);
- LinearLayout.LayoutParams LP_LIKE_MW = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
- tv_like.setGravity(Gravity.CENTER);
- tv_like.setPadding(0, 8, 0, 8);
- tv_like.setText("赞(8)");
- tv_like.setTextSize(16);
- layout_sub_Lin.addView(tv_like, LP_LIKE_MW);
上源码(两个例子代码混合在一起):http://download.csdn.net/detail/harvic880925/6829633 (不要分,仅供分享)
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/18042183 谢谢!!!
动态添加控件及将某XML动态加入到Activity显示相关推荐
- 动态添加综合布局---动态添加控件及将某XML动态加入到Activity显示(续)
前言:以前曾写过一篇关于动态生成控件的文章<动态添加控件及将某XML动态加入到Activity显示>,比较浅显,对于RelativeLayout的相关布局设置方法及相对布局与线性布局的混合 ...
- js动态添加控件服务器响应,JS实现动态给标签控件添加事件的方法示例
本文实例讲述了JS实现动态给标签控件添加事件的方法.分享给大家供大家参考,具体如下: /p> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-trans ...
- asp.net动态添加控件学习
看了老师的教程后,自己一点感悟记录下来: 1.在页面提交后,动态生成的控件会丢失, 但如果生成控件的代码在pageload中,就可以,原理是每次生成页面都执行生成. 2.动态按件或页面原来控件, 在页 ...
- jQuery EasyUI动态添加控件或者ajax加载页面后不能自动渲染问题的解决方法
博客分类: jquery-easyui jQueryAjax框架HTML 现象: AJAX返回的html无法做到自动渲染为EasyUI的样式.比如:class="easyui-layout ...
- 关于页面动态添加控件(转)
最近看到很多问题都是关于页面动态添加控件,页面postback以后,就取不到这些控件了.其实每次在页面上动态添加控件(或者在数据控件上动态添加列),它们的状态是不会被添加到页面的ViewState中去 ...
- Android 在布局容器中动态添加控件
这里,通过一个小demo,就可以掌握在布局容器中动态添加控件,以动态添加Button控件为例,添加其他控件同样道理. 1.addView 添加控件到布局容器 2.removeView 在布局容器中删掉 ...
- Windows phone7 动态添加控件
Windows phone7 动态添加控件 2011-03-29 3:48 using System; using System; using System.Collections.Generic; ...
- 学习笔记:VB.net动态添加控件数组并传递事件
学习笔记:VB.net动态添加控件数组并传递事件 控件数组和事件 "中间人" 动态添加控件 控件数组和事件 新建一个用户窗体,在定义控件数组时,不能用Withevnets来定义数组 ...
- Winform Panel动态添加控件坐标原点问题
Winform Panel动态添加控件坐标原点问题 Panel的坐标原点是在左上角,是在可视区域的左上角,当滚轮往下移动的时候,坐标原点也会跟着移动,所以动态添加控件的时候,在可视区域的时候控件的距离 ...
最新文章
- 遗传算法来控制进入(一)
- 帝国理工学院(IC)研究人员设计了一种可以解决瘫痪的脑机设备
- 软件包管理 之 file.src.rpm 使用方法的简单介绍
- BeautifulSoup([your markup]) to this: BeautifulSoup([your markup], lxml) 解决未设置默认解析器的错误
- php 递归删除函数
- 【CV】综述:基于深度学习的视觉跟踪方法进展
- 【机器学习】4 款超级棒的模型可解释性工具包,总有一款适合你!
- python对象group函数_如何对groupby对象应用多个函数
- unity 是厘米还是米_身高差45厘米! 这对重庆小情侣拍短视频晒幸福火了
- angular - 如何运行在起来 - 使用nginx
- laravel redis_thinkphp+redis实现秒杀,缓存等功能
- Impala-shell 查询异常 - ERROR: AnalysisException: Failed to evaluate expr: 1
- [转]如何设置win7一直以管理员身份运行
- pandas分组计算平均值_pandas索引,分组计算
- 《深入理解 Spring Cloud 与微服务构建》第十三章 配置中心 Spring Cloud Config
- Given a list,rotate the list to right by k places, where k is nonegative.
- 团队冲刺第一阶段第九天
- smarty php5.5,smarty如何完美兼容php5.5和preg_replace_callback如何替换preg_r
- Python3——使用exec和eval执行字符串及计算其结果
- Windows下FireFox插件dll文件名,必须是np开头,不能是plugin结尾
热门文章
- 【错误记录】Android Studio 的 Flutter 代码界面没有 Logcat 面板 ( 2021年08月28日最新解决方案 )
- 【OkHttp】OkHttp 简介 ( OkHttp 框架特性 | Http 版本简介 )
- 【Android 文件管理】应用可访问的存储空间 ( 存储空间分类 | 存储空间访问权限 | 分区存储 )
- 【Kotlin】扩展函数 ( 扩展函数声明 | 调用方式 | 与成员函数优先级 | 接收者空值处理 )
- 理解面向对象编写的极致简单的小代码
- Java安全管理器――SecurityManager
- 结合自己造的轮子实践按需加载
- Mysql数据库的打开和关闭
- springMVC 处理json 及 HttpMessageConverter 接口
- HTTPS_SSL配置的步骤以及原理说明