android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法
本文實(shí)例講述了android動(dòng)態(tài)布局之動(dòng)態(tài)加入TextView和ListView的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
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("常用聯(lián)系人");
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);
// 線性布局 通過參數(shù)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<Map<String, Object>> data2 = new ArrayList<Map<String, Object>>();
Map<String, Object> map2 = new HashMap<String, Object>();
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("常用聯(lián)系人");
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); // 線性布局 通過參數(shù)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<String, ?>> mData;
private int mResource;
private LayoutInflater mInflater;
public ImageAndTextAdapter(Context context,
List<? extends Map<String, ?>> 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 < count; 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<String, ?> 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 < count; 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
<?xml version="1.0" encoding="UTF-8"?> <!-- 好友信息列表.xml --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/selfImage" 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" /> <ImageView android:id="@+id/currentStatus" 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" /> <TextView android:id="@+id/setStatus" android:layout_width="wrap_content" 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" /> </RelativeLayout> <EditText android:id="@+id/searchFriend" android:adjustViewBounds="true" android:layout_height="50dip" android:layout_width="fill_parent" android:text="Search..." /> <!-- 好友組 點(diǎn)擊textview后出現(xiàn)組里的詳細(xì)好友列表 --> <LinearLayout android:id="@+id/groups" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </LinearLayout>
chats_view_item.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/white"> <RelativeLayout android:id="@+id/chats_view_item" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/chats_view_item_image" 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" /> <TextView android:id="@+id/chats_view_name" android:textSize="14.0sp" 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" /> <ImageView android:id="@+id/friend_status_icon" 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" /> <TextView android:id="@+id/chats_view_status" 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" /> </RelativeLayout> </LinearLayout>
效果圖如下:

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- android 通過向viewpage中添加listview來完成滑動(dòng)效果(類似于qq滑動(dòng)界面)
- android Listview模擬聊天界面
- Android 新聞界面模擬ListView和ViewPager的應(yīng)用
- Android ListView自定義Adapter實(shí)現(xiàn)仿QQ界面
- Android App界面的ListView布局實(shí)戰(zhàn)演練
- Android中使用Expandablelistview實(shí)現(xiàn)微信通訊錄界面
- Android ListView添加頭布局和腳布局實(shí)例詳解
- Android實(shí)現(xiàn)的ListView分組布局改進(jìn)示例
- 神奇的listView實(shí)現(xiàn)自動(dòng)顯示隱藏布局Android代碼
- Android開發(fā)之ListView的簡(jiǎn)單用法及定制ListView界面操作示例
相關(guān)文章
android+json+php+mysql實(shí)現(xiàn)用戶反饋功能方法解析
相信每個(gè)項(xiàng)目都會(huì)有用戶反饋建議等功能,這個(gè)實(shí)現(xiàn)的方法很多,下面是我實(shí)現(xiàn)的方法,供大家交流2012-11-11
Android開發(fā)實(shí)現(xiàn)錄屏小功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)錄屏小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android自定義控件實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填充
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填充,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)多點(diǎn)觸摸應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android 自定義彈性ListView控件實(shí)例代碼(三種方法)
關(guān)于在Android中實(shí)現(xiàn)ListView的彈性效果,有很多不同的方法,網(wǎng)上一搜,也有很多,下面貼出在項(xiàng)目中經(jīng)常用到的兩種實(shí)現(xiàn)ListView彈性效果的方法(基本上拿來就可以用),需要的朋友參考下本段代碼2016-01-01
Flutter音樂播放插件audioplayers使用步驟詳解
audioplayers是一個(gè)可以支持同時(shí)播放多個(gè)音頻文件的Flutter的插件,可以播放多個(gè)同時(shí)的音頻文件,這篇文章主要介紹了audioplayers的使用步驟,感興趣想要詳細(xì)了解可以參考下文2023-05-05
Android實(shí)現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)
本篇文章主要介紹了Android實(shí)現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解
本篇文章主要介紹了Android學(xué)習(xí)筆記之ListView復(fù)用機(jī)制詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02

