热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android动态建立textview,android动态布局之动态加入TextView和ListView的方法

本文实例讲述了android动态布局之动态加入TextView和ListView的方法。分享给大家供大家参考。具体实现方法如下:packageorg.guoshi;i

本文实例讲述了android动态布局之动态加入TextView和ListView的方法。分享给大家供大家参考。具体实现方法如下:

package org.guoshi;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.guoshi.adapter.ImageAndTextAdapter;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup.LayoutParams;

import android.widget.LinearLayout;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.RelativeLayout;

import android.widget.TextView;

public class Main extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.friend_info_view);

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.groups);

final ListView lv = new ListView(this);

List> data = new ArrayList>();

Map map = new HashMap();

map.put("title", "jayqean");

map.put("imgsrc", R.drawable.icon);

data.add(map);

ListAdapter adapter = new ImageAndTextAdapter(Main.this, data, R.layout.chats_view_item, new String[] { "title", "imgsrc" }, new int[] {

R.id.chats_view_name,

R.id.chats_view_item_image });

lv.setAdapter(adapter);

final TextView tv1 = new TextView(this);

tv1.setText("常用联系人");

tv1.setId(1);

final RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

lp1.addRule(RelativeLayout.BELOW, R.id.groups);

tv1.setLayoutParams(lp1);

tv1.setBackgroundColor(R.color.group_view_background);

tv1.setOnClickListener(new OnClickListener() {

boolean flag = false;

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Log.d("tag", tv1.getText().toString());

if(!flag){

linearLayout.addView(lv, linearLayout.indexOfChild(tv1) + 1);

// lp1.addRule(RelativeLayout.BELOW, 1);

// linearLayout.addView(lv, lp1);

flag = true;

} else{

linearLayout.removeView(lv);

flag = false;

}

}

});

linearLayout.addView(tv1, lp1);

// 线性布局 通过参数index控制加入的控件的位置

// ------------------------

// 加入分割线

final TextView line = new TextView(this);

line.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 1));

line.setBackgroundColor(Color.WHITE);

linearLayout.addView(line, 1);

// ------------------------

final ListView lv2 = new ListView(this);

List> data2 = new ArrayList>();

Map map2 = new HashMap();

map2.put("title", "xiaobei");

map2.put("imgsrc", R.drawable.icon);

data2.add(map2);

ListAdapter adapter2 = new ImageAndTextAdapter(Main.this, data2, R.layout.chats_view_item, new String[] { "title", "imgsrc" }, new int[] {

R.id.chats_view_name,

R.id.chats_view_item_image });

lv2.setAdapter(adapter2);

final TextView tv2 = new TextView(this);

tv2.setText("离线好友");

tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

tv2.setBackgroundColor(R.color.group_view_background);

tv2.setOnClickListener(new OnClickListener() {

boolean flag = false;

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Log.d("tag", tv2.getText().toString());

if(!flag){

linearLayout.addView(lv2, linearLayout.indexOfChild(tv2) + 1);

flag = true;

} else{

linearLayout.removeView(lv2);

flag = false;

}

}

});

linearLayout.addView(tv2, 2);

}

}

控制布局,可以通过RelativeLayout.LayoutParams类

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.groups);

final TextView tv1 = new TextView(this);

tv1.setText("常用联系人");

RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

lp1.addRule(RelativeLayout.BELOW, R.id.groups);

tv1.setLayoutParams(lp1);

linearLayout.addView(tv1, lp1);

也可采用linearLayout.addView(tv1, 0); // 线性布局 通过参数index控制加入的控件的位置

package org.guoshi.adapter;

import java.util.List;

import java.util.Map;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Checkable;

import android.widget.ImageView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

public class ImageAndTextAdapter extends SimpleAdapter {

private Context mcontext;

private int[] mTo;

private String[] mFrom;

private ViewBinder mViewBinder;

private List extends Map> mData;

private int mResource;

private LayoutInflater mInflater;

public ImageAndTextAdapter(Context context,

List extends Map> data, int resource, String[] from,

int[] to) {

super(context, data, resource, from, to);

mcontext = context;

mData = data;

mResource = resource;

mFrom = from;

mTo = to;

mInflater = (LayoutInflater) context

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// mInflater = LayoutInflater.from(mcontext);

}

/**

* @see android.widget.Adapter#getView(int, View, ViewGroup)

*/

public View getView(int position, View convertView, ViewGroup parent) {

return createViewFromResource(position, convertView, parent, mResource);

}

private View createViewFromResource(int position, View convertView,

ViewGroup parent, int resource) {

View v;

if (convertView == null) {

v = mInflater.inflate(resource, parent, false);

final int[] to = mTo;

final int count = to.length;

final View[] holder = new View[count];

for (int i = 0; i

holder[i] = v.findViewById(to[i]);

}

v.setTag(holder);

} else {

v = convertView;

}

bindView(position, v);

// final int index = position;

// v.setOnClickListener(new OnClickListener() {

//

// public void onClick(View v) {

// // TODO Auto-generated method stub

// Log.d("item", index + "");

// }

// });

return v;

}

private void bindView(int position, View view) {

final Map dataSet = mData.get(position);

if (dataSet == null) {

return;

}

final ViewBinder binder = mViewBinder;

final View[] holder = (View[]) view.getTag();

final String[] from = mFrom;

final int[] to = mTo;

final int count = to.length;

for (int i = 0; i

final View v = holder[i];

if (v != null) {

final Object data = dataSet.get(from[i]);

String text = data == null ? "" : data.toString();

if (text == null) {

text = "";

}

boolean bound = false;

if (binder != null) {

bound = binder.setViewValue(v, data, text);

}

if (!bound) {

if (v instanceof Checkable) {

if (data instanceof Boolean) {

((Checkable) v).setChecked((Boolean) data);

} else {

throw new IllegalStateException(v.getClass()

.getName()

+ " should be bound to a Boolean, not a "

+ data.getClass());

}

} else if (v instanceof TextView) {

setViewText((TextView) v, text);

} else if (v instanceof ImageView) {

if (data instanceof Integer) {

setViewImage((ImageView) v, (Integer) data);

} else {

setViewImage((ImageView) v, text);

}

} else {

throw new IllegalStateException(

v.getClass().getName()

+ " is not a "

+ " view that can be bounds by this SimpleAdapter");

}

}

}

}

}

/**

* Called by bindView() to set the image for an ImageView but only if there

* is no existing ViewBinder or if the existing ViewBinder cannot handle

* binding to an ImageView.

*

* This method is called instead of {@link #setViewImage(ImageView, String)}

* if the supplied data is an int or Integer.

*

* @param v

* ImageView to receive an image

* @param value

* the value retrieved from the data set

*

* @see #setViewImage(ImageView, String)

*/

public void setViewImage(ImageView v, int value) {

v.setImageResource(value);

}

/**

* Called by bindView() to set the image for an ImageView but only if there

* is no existing ViewBinder or if the existing ViewBinder cannot handle

* binding to an ImageView.

*

* By default, the value will be treated as an image resource. If the value

* cannot be used as an image resource, the value is used as an image Uri.

*

* This method is called instead of {@link #setViewImage(ImageView, int)} if

* the supplied data is not an int or Integer.

*

* @param v

* ImageView to receive an image

* @param value

* the value retrieved from the data set

*

* @see #setViewImage(ImageView, int)

*/

public void setViewImage(ImageView v, String value) {

Bitmap bitMap = BitmapFactory.decodeFile(value);

v.setImageBitmap(bitMap);

}

}

下面是friend_info_view.xml

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:background="#ffffff">

android:layout_height="wrap_content">

android:adjustViewBounds="true" android:layout_width="@dimen/self_image_width"

android:layout_height="@dimen/self_image_height"

android:layout_marginLeft="5.0dip" android:layout_marginBottom="10.0dip"

android:layout_marginTop="3.0dip" android:src="@drawable/default_image" />

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:src="@drawable/status_available" android:layout_marginLeft="8.0dip"

android:layout_marginTop="20.0dip" android:layout_toRightOf="@id/selfImage" />

android:layout_height="wrap_content" android:layout_marginTop="20.0dip"

android:layout_marginLeft="8.0dip" android:text="Tap here to set your status"

android:layout_toRightOf="@+id/currentStatus" />

android:adjustViewBounds="true" android:layout_height="50dip"

android:layout_width="fill_parent" android:text="Search..." />

android:layout_height="wrap_content" android:orientation="vertical" >

chats_view_item.xml

android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" android:background="@color/white">

android:layout_width="wrap_content" android:layout_height="wrap_content">

android:layout_width="@dimen/friend_image_width"

android:layout_height="@dimen/friend_image_height"

android:paddingLeft="5.0dip" android:paddingTop="2.0dip"

android:src="@drawable/default_image" />

android:paddingLeft="10.0dip" android:textStyle="bold"

android:ellipsize="marquee" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="username"

android:singleLine="true" android:paddingTop="2.0dip"

android:layout_toRightOf="@+id/chats_view_item_image" />

android:layout_width="wrap_content" android:layout_height="wrap_content"

android:paddingLeft="10.0dip" android:paddingTop="1.0dip"

android:layout_below="@+id/chats_view_name" android:layout_toRightOf="@+id/chats_view_item_image"

android:src="@drawable/jabber_available" />

android:textColor="@android:color/secondary_text_light"

android:ellipsize="marquee" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="available"

android:singleLine="true" android:paddingLeft="2.0dip"

android:layout_toRightOf="@+id/friend_status_icon"

android:layout_below="@+id/chats_view_name" />

效果图如下:

97441192981403623b62daee72ae33e4.gif

希望本文所述对大家的Android程序设计有所帮助。



推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
author-avatar
豆豆bo69_550
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有