当前位置:文档之家› android中动态布局

android中动态布局

android中动态布局
android中动态布局

android中动态布局(动态加入TextView和ListView)

分类:Android2011-04-21 16:20 1068人阅读评论(1) 收藏举报[java]view plaincopy

1.package org.guoshi;

2.import java.util.ArrayList;

3.import java.util.HashMap;

4.import java.util.List;

5.import java.util.Map;

6.import org.guoshi.adapter.ImageAndTextAdapter;

7.import android.app.Activity;

8.import android.graphics.Color;

9.import android.os.Bundle;

10.import android.util.Log;

11.import android.view.View;

12.import android.view.View.OnClickListener;

13.import https://www.doczj.com/doc/0c9620897.html,youtParams;

14.import android.widget.LinearLayout;

15.import android.widget.ListAdapter;

16.import android.widget.ListView;

17.import android.widget.RelativeLayout;

18.import android.widget.TextView;

19.public class Main extends Activity {

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

21.@Override

22.public void onCreate(Bundle savedInstanceState) {

23.super.onCreate(savedInstanceState);

24. setContentView(https://www.doczj.com/doc/0c9620897.html,yout.friend_info_view);

25.final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.g

roups);

26.

27.final ListView lv = new ListView(this);

28. List> data = new ArrayList>(

);

29. Map map = new HashMap();

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

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

32. data.add(map);

33. ListAdapter adapter = new ImageAndTextAdapter(Main.this, data, https://www.doczj.com/doc/0c9620897.html,y

out.chats_view_item, new String[] { "title", "imgsrc" }, new int[] {

34. R.id.chats_view_name,

35. R.id.chats_view_item_image });

36. lv.setAdapter(adapter);

37.final TextView tv1 = new TextView(this);

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

39. tv1.setId(1);

40.final https://www.doczj.com/doc/0c9620897.html,youtParams lp1 = new https://www.doczj.com/doc/0c9620897.html,youtPar

ams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

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

42. tv1.setLayoutParams(lp1);

43. tv1.setBackgroundColor(R.color.group_view_background);

44. tv1.setOnClickListener(new OnClickListener() {

45.boolean flag = false;

46.@Override

47.public void onClick(View v) {

48.// TODO Auto-generated method stub

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

50.if(!flag){

51. linearLayout.addView(lv, linearLayout.indexOfChild(tv1)

+ 1);

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

53.// linearLayout.addView(lv, lp1);

54. flag = true;

55. } else{

56. linearLayout.removeView(lv);

57. flag = false;

58. }

59.

60. }

61. });

62. linearLayout.addView(tv1, lp1); // 线性布局通过参数index控制加入的控

件的位置

63.// ------------------------

64.// 加入分割线

65.final TextView line = new TextView(this);

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

67. line.setBackgroundColor(Color.WHITE);

68. linearLayout.addView(line, 1);

69.// ------------------------

70.final ListView lv2 = new ListView(this);

71. List> data2 = new ArrayList>

();

72. Map map2 = new HashMap();

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

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

75. data2.add(map2);

76. ListAdapter adapter2 = new ImageAndTextAdapter(Main.this, data2, R.l

ayout.chats_view_item, new String[] { "title", "imgsrc" }, new int[] {

77. R.id.chats_view_name,

78. R.id.chats_view_item_image });

79. lv2.setAdapter(adapter2);

80.

81.final TextView tv2 = new TextView(this);

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

83. tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, Layou

tParams.WRAP_CONTENT));

84. tv2.setBackgroundColor(R.color.group_view_background);

85. tv2.setOnClickListener(new OnClickListener() {

86.boolean flag = false;

87.@Override

88.public void onClick(View v) {

89.// TODO Auto-generated method stub

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

91.if(!flag){

92. linearLayout.addView(lv2, linearLayout.indexOfChild(tv2)

+ 1);

93. flag = true;

94. } else{

95. linearLayout.removeView(lv2);

96. flag = false;

97. }

98.

99. }

100. });

101. linearLayout.addView(tv2, 2);

102.

103.

104. }

105.}

控制布局,可以通过https://www.doczj.com/doc/0c9620897.html,youtParams类

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

final TextView tv1 = new TextView(this);

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

https://www.doczj.com/doc/0c9620897.html,youtParams lp1 = new

https://www.doczj.com/doc/0c9620897.html,youtParams(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控制加入的控件的位置[java]view plaincopy

1.package org.guoshi.adapter;

2.import java.util.List;

3.import java.util.Map;

4.import android.content.Context;

5.import android.graphics.Bitmap;

6.import android.graphics.BitmapFactory;

7.import android.util.Log;

8.import https://www.doczj.com/doc/0c9620897.html,youtInflater;

9.import android.view.View;

10.import android.view.View.OnClickListener;

11.import android.view.ViewGroup;

12.import android.widget.Checkable;

13.import android.widget.ImageView;

14.import android.widget.SimpleAdapter;

15.import android.widget.TextView;

16.public class ImageAndTextAdapter extends SimpleAdapter {

17.private Context mcontext;

18.private int[] mTo;

19.private String[] mFrom;

20.private ViewBinder mViewBinder;

21.

22.private List> mData;

23.private int mResource;

24.private LayoutInflater mInflater;

25.public ImageAndTextAdapter(Context context,

26. List> data, int resource, String[] from

,

27.int[] to) {

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

29. mcontext = context;

30. mData = data;

31. mResource = resource;

32. mFrom = from;

33. mTo = to;

34. mInflater = (LayoutInflater) context

35. .getSystemService(https://www.doczj.com/doc/0c9620897.html,YOUT_INFLATER_SERVICE);

36.// mInflater = LayoutInflater.from(mcontext);

37. }

38./**

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

40. */

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

42.return createViewFromResource(position, convertView, parent, mResour

ce);

43. }

44.private View createViewFromResource(int position, View convertView,

45. ViewGroup parent, int resource) {

46. View v;

47.if (convertView == null) {

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

49.final int[] to = mTo;

50.final int count = to.length;

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

52.for (int i = 0; i < count; i++) {

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

54. }

55. v.setTag(holder);

56. } else {

57. v = convertView;

58. }

59.

60. bindView(position, v);

61.

62.// final int index = position;

63.// v.setOnClickListener(new OnClickListener() {

64.//

65.// public void onClick(View v) {

66.// // TODO Auto-generated method stub

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

68.// }

69.// });

70.return v;

71. }

72.

73.private void bindView(int position, View view) {

74.

75.final Map dataSet = mData.get(position);

76.if (dataSet == null) {

77.return;

78. }

79.final ViewBinder binder = mViewBinder;

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

81.final String[] from = mFrom;

82.final int[] to = mTo;

83.final int count = to.length;

84.for (int i = 0; i < count; i++) {

85.final View v = holder[i];

86.if (v != null) {

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

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

89.if (text == null) {

90. text = "";

91. }

92.boolean bound = false;

93.if (binder != null) {

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

95. }

96.if (!bound) {

97.if (v instanceof Checkable) {

98.if (data instanceof Boolean) {

99. ((Checkable) v).setChecked((Boolean) data); 100. } else {

101.throw new IllegalStateException(v.getClass() 102. .getName()

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

104. + data.getClass());

105. }

106. } else if (v instanceof TextView) {

107. setViewText((TextView) v, text);

108. } else if (v instanceof ImageView) {

109.if (data instanceof Integer) {

110. setViewImage((ImageView) v, (Integer) data); 111. } else {

112. setViewImage((ImageView) v, text);

113. }

114. } else {

115.throw new IllegalStateException(

116. v.getClass().getName()

117. + " is not a "

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

119. }

120. }

121. }

122. }

123. }

124./**

125. * Called by bindView() to set the image for an ImageView but only if t here

126. * is no existing ViewBinder or if the existing ViewBinder cannot handl e

127. * binding to an ImageView.

128. *

129. * This method is called instead of {@link #setViewImage(ImageView, Str ing)}

130. * if the supplied data is an int or Integer.

131. *

132. * @param v

133. * ImageView to receive an image

134. * @param value

135. * the value retrieved from the data set

136. *

137. * @see #setViewImage(ImageView, String)

138. */

139.public void setViewImage(ImageView v, int value) {

140. v.setImageResource(value);

141. }

142./**

143. * Called by bindView() to set the image for an ImageView but only if t here

144. * is no existing ViewBinder or if the existing ViewBinder cannot handl e

145. * binding to an ImageView.

146. *

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

148. * cannot be used as an image resource, the value is used as an image U ri.

149. *

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

151. * the supplied data is not an int or Integer.

152. *

153. * @param v

154. * ImageView to receive an image

155. * @param value

156. * the value retrieved from the data set

157. *

158. * @see #setViewImage(ImageView, int)

159. */

160.public void setViewImage(ImageView v, String value) {

161. Bitmap bitMap = BitmapFactory.decodeFile(value);

162. v.setImageBitmap(bitMap);

163. }

164.}

下面是friend_info_view.xml

[xhtml]view plaincopy

1.

2.

3.

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

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

6.

7.android:layout_height="wrap_content">

8.

9.android:adjustViewBounds="true"android:layout_width="@dimen/sel

f_image_width"

10.android:layout_height="@dimen/self_image_height"

11.android:layout_marginLeft="5.0dip"android:layout_marginBottom="

10.0dip"

12.android:layout_marginTop="3.0dip"android:src="@drawable/default

_image"/>

13.

14.android:layout_width="wrap_content"android:layout_height="wrap_

content"

15.android:src="@drawable/status_available"android:layout_marginLe

ft="8.0dip"

16.android:layout_marginTop="20.0dip"android:layout_toRightOf="@id

/selfImage"/>

17.

18.android:layout_height="wrap_content"android:layout_marginTop="2

0.0dip"

19.android:layout_marginLeft="8.0dip"android:text="Tap here to set

your status"

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

21.

22.

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

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

25.

26.

27.android:layout_height="wrap_content"android:orientation="vertical"

>

28.

29.

30.

chats_view_item.xml

[xhtml]view plaincopy

1.

2.

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

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

5.

6.android:layout_width="wrap_content"android:layout_height="wrap_cont

ent">

7.

8.android:layout_width="@dimen/friend_image_width"

9.android:layout_height="@dimen/friend_image_height"

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

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

12.

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

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

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

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

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

18.

19.android:layout_width="wrap_content"android:layout_height="wrap_

content"

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

21.android:layout_below="@+id/chats_view_name"android:layout_toRig

htOf="@+id/chats_view_item_image"

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

23.

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

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

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

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

28.android:layout_toRightOf="@+id/friend_status_icon"

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

30.

31.

效果图:

Android布局属性大全

Android布局属性大全 布局: AbsoluteLayout(绝对布局): xmlns:android="https://www.doczj.com/doc/0c9620897.html,/apk/res/android" style="@..." android:clipChildren="true|false" android:clipToPadding="true|false" android:layoutAnimation="@---" android:animationCache="true|false" android:persistentDrawingCache="none|animation|scrolling|all":持续高速缓存绘图 android:alwaysDrawnWithCache="true|false" android:andStatesFromChildre="true|false" android:descendantFocusability="beforeDescendants|afterDescendants|bl ocksDescendants":后裔可聚焦 android:id="@+id/absoluteLayout" android:tag="@---" android:android:scrollX="---" android:android:scrollY="---" android:background="@---" android:padding="----" android:paddingLeft="----" android:paddingTop="----" android:paddingRight="----" android:paddingBotton="---" android:focusable="true|false" android:focusableInTouchMode="true|false" android:visibility="visible|invisible|gone" android:fitsSystemWindows="true|false":适合系统窗口 android:scrollbars="none|horizontal|vertical" android:scrollbarStyle="insideOverlay(内覆盖)|insideInset(内插 图)|outsideOverlay(外覆盖)|outsideInset(外插图)" android:isScrollContainer="true|false":是一个滚动集合 android:fadeScrollbars="true|false":褪色的滚动条 android:scrollbarFadeDuration="---":卷轴淡出 android:scrollDefaultDelayBeforeFade="---":滚动前默认延迟 android:scrollbarSize="---" android:scrollbarThumbHorizontal="@----":拇指水平滚动条 android:scrollbarThumbVertical="@----":拇指垂直滚动条 android:scrollbarTrackVertical="@---":垂直滚动条轨道 android:scrollbarTrackHorizontal="@---":水平滚动条轨道 android:scrollbarAlwaysDrawHorizontalTrack="true|false":水平滚动条总是吸引轨道

Android 开发之旅-view的几种布局方式及实践

Android 开发之旅:view的几种布局方式及实践 https://www.doczj.com/doc/0c9620897.html,/1943397/363552 2010-06-06 14:14:00 标签:布局 View 实践移动开发 Android 原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。 https://www.doczj.com/doc/0c9620897.html,/1943397/363552 引言 通过前面两篇: 1.Android 开发之旅:又见Hello World! 2.Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理及布局文件可谓有了比较深刻的认识和理解,并且用“Hello World!”程序来实践证明了。在继续深入Android开发之旅之前,有必要解决前两篇中没有介绍的遗留问题:View的几种布局显示方法,以后就不会在针对布局方面做过多的介绍。View的布局显示方式有下面几种:线性布局(Linear Layout)、相对布局(Relative Layout)、表格布局(Table Layout)、网格视图(Grid View)、标签布局(Tab Layout)、列表视图(List View)、绝对布局(AbsoluteLayout)。本文虽然是介绍View的布局方式,但不仅仅是这样,其中涉及了很多小的知识点,绝对能给你带来Android大餐! 本文的主要内容就是分别介绍以上视图的七种布局显示方式效果及实现,大纲如下: 1.1、View布局概述 2.2、线性布局(Linear Layout) 1. 2.1、Tips:android:layout_weight="1" 3.3、相对布局(Relative Layout) 4.4、表格布局(Table Layout) 5.5、列表视图(List View) 1. 5.1、一个小的改进 2. 5.2、补充说明 6.6、网格视图(Grid View) 7.7 、绝对布局() 8.8、标签布局(Tab Layout) 1、view的布局显示概述 通过前面的学习我们知道:在一个Android应用程序中,用户界面通过View和ViewGroup对象构建。Android中有很多种View和ViewGroup,他们都继承自View类。View对象是Android平台上表示用户界面的基本单元。 View的布局显示方式直接影响用户界面,View的布局方式是指一组View元素如何布局,准确的说是一个ViewGroup中包含的一些View怎么样布局。ViewGroup类是布局(layout)和视图容器(View container)的基类,此类也定义了https://www.doczj.com/doc/0c9620897.html,youtParams类,它作为布局参数的基类,此类告诉父视图其中的子视图想如何显示。例如,XML布局文件中名为layout_something的属性(参加上篇的4.2节)。我们要介绍的View的布局方式的类,都是直接或间接继承自ViewGroup类,如下图所示:

Android七种布局解析

我们对Android应用程序运行原理及布局文件可谓有了比较深刻的认识和理解,并且用“Hello World!” 程序来实践证明了。在继续深入Android开发之旅之前,有必要解决前两篇中没有介绍的遗留问题:View 的几种布局显示方法,以后就不会在针对布局方面做过多的介绍。View的布局显示方式有下面几种: 线性布局(Linear Layout)、 相对布局(Relative Layout)、 表格布局(Table Layout)、 网格视图(Grid View)、 标签布局(Tab Layout)、 列表视图(List View)、 绝对布局(AbsoluteLayout)。本文虽然是介绍View的布局方式,但不仅仅是这样,其中涉及了很多小的知识点,绝对能给你带来Android大餐! 本文的主要内容就是分别介绍以上视图的七种布局显示方式效果及实现,大纲如下: ?1、View布局概述 ?2、线性布局(Linear Layout) o 2.1、Tips:android:layout_weight="1" ?3、相对布局(Relative Layout) ?4、表格布局(Table Layout) ?5、列表视图(List View) o 5.1、一个小的改进 o 5.2、补充说明 ?6、网格视图(Grid View) ?7 、绝对布局() ?8、标签布局(Tab Layout) 1、view的布局显示概述 通过前面的学习我们知道:在一个Android应用程序中,用户界面通过View和ViewGroup对象构建。A ndroid中有很多种View和ViewGroup,他们都继承自View类。View对象是Android平台上表示用户界面的基本单元。 View的布局显示方式直接影响用户界面,View的布局方式是指一组View元素如何布局,准确的说是一个ViewGroup中包含的一些View怎么样布局。ViewGroup类是布局(layout)和视图容器(View containe r)的基类,此类也定义了https://www.doczj.com/doc/0c9620897.html,youtParams类,它作为布局参数的基类,此 类告诉父视图其中的子视图想如何显示。例如,XML布局文件中名为layout_so mething的属性(参加上篇的4.2节)。我们要介绍的View的布局方式的类,都是直接或间接继承自ViewGroup类,如下图所示:

Android_布局详解【图文】

Android 布局详解【图文】 Android 布局是开发中非常重要的一个知识部分,它的布局分为以下几种: Linear Layout:线性布局 Relative Layout:相对布局 Table Layout:表格布局 FrameLayout AbsoluteLayout Grid View:网格布局 Tab Layout:选项卡布局 List View:列表布局 一、Linear Layout 简单来说,直着排,横着排都可以,还可以嵌套,此布局运用的非常多。下面直接上示例代码及截图:

接下来,看一下布局XML文件: