Android 动态添加View
来源:互联网 发布:淘宝店铺被违规警告 编辑:程序博客网 时间:2024/05/21 10:08
目前安卓的控件中,ListView可以动态添加显示内容。但是我遇到了这样一个问题,在一个页面中,已经有了一个
ScrollView,在xml布局中,它提示我不能有两个纵向滑动的控件(大概是这个意思),我还想添加一个数目不定的
同一类View,后来根据Listview动态展示内容的方式,自己实现了动态添加View的功能。
布局文件的一部分代码:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginLeft="15dip" android:layout_marginRight="15dip" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="2dip" android:layout_height="15dip" android:background="@drawable/icon_some_museum_title_txt" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="6dip" android:textSize="14sp" android:textColor="@color/some_museum_title_text_color" android:text="@string/exhibitionhall_introduction" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="2dip" android:background="@drawable/pic_some_museum_exhibitionhall_line" android:layout_marginTop="4dip" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:id="@+id/themehall_introduction" > </LinearLayout>
上面的代码中,
LinearLayout
android:id=”@+id/themehall_introduction”
是预留的,用来动态添加布局的,类似ListView中的ListView … android:id=”@+id/listview”
,用来动态添加展示的内容。
要动态添加到这个地方的View是这样的:apart_dynamic_load_themehall.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="wrap_content" android:orientation="horizontal" android:layout_marginLeft="3dip" android:layout_marginTop="3dip" android:baselineAligned="false" android:background="@drawable/btn_layout_click_bg"> <RelativeLayout android:layout_weight="1" android:layout_width="0dip" android:layout_height="90dip" android:layout_marginLeft="10dip" android:layout_marginRight="3dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="@drawable/pic_some_museum_botany" android:id="@+id/inreoduce_themehall1" > <TextView android:layout_width="match_parent" android:layout_height="18dip" android:background="#7F000000" android:text="@string/exhibitionhall_botany" android:textSize="12sp" android:textColor="#FFFFFF" android:layout_alignParentBottom="true" android:paddingLeft="5dip" android:id="@+id/tv_themehall_name1" /> </RelativeLayout> <RelativeLayout android:layout_weight="1" android:layout_width="0dip" android:layout_height="90dip" android:layout_marginLeft="10dip" android:layout_marginRight="3dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:background="@drawable/pic_some_museum_dinosaur_park" android:id="@+id/inreoduce_themehall2" > <TextView android:layout_width="match_parent" android:layout_height="18dip" android:background="#7F000000" android:text="@string/exhibitionhall_dinosaur_park" android:textSize="12sp" android:textColor="#FFFFFF" android:layout_alignParentBottom="true" android:paddingLeft="5dip" android:id="@+id/tv_themehall_name2" /> </RelativeLayout></LinearLayout>
前期工作准备好了,那么久开始上真正的代码了
private void initLinearLayoutThemehall(){ LayoutInflater mInflater = SomeMuseumActivity.this.getLayoutInflater(); try { listThemehall = RDB_ThemehallImpl.getRThemehallList(SomeMuseumActivity.this, lrMuseum.getMuseumHashCode()); if (listThemehall!=null && listThemehall.size()>0) { for (int i = 0; i < listThemehall.size(); i++) { if (i%2==1) { initThemehallViewDouble(mInflater,i); } if (listThemehall.size()%2==1 && listThemehall.size()-1 == i) { initTemehallViewSingle(mInflater,i); } } } } catch (DBException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
上面这部分代码是在onCreate方法中调用的,是动态添加View的入口。if (i%2==1) {
initThemehallViewDouble(mInflater,i);
}
if (listThemehall.size()%2==1 && listThemehall.size()-1 == i) {
initTemehallViewSingle(mInflater,i);
}
是控制展示样式的。
具体动态添加的代码:
@SuppressLint("NewApi") private void initThemehallViewDouble(LayoutInflater mInflater,final int item){ View view = mInflater.inflate(R.layout.apart_dynamic_load_themehall, null); RelativeLayout r1 = (RelativeLayout) view.findViewById(R.id.inreoduce_themehall1); RelativeLayout r2 = (RelativeLayout) view.findViewById(R.id.inreoduce_themehall2); TextView tv1 = (TextView) view.findViewById(R.id.tv_themehall_name1); TextView tv2 = (TextView) view.findViewById(R.id.tv_themehall_name2); if (listThemehall.get(item-1)!=null) { String tHashCode = listThemehall.get(item-1).getThemeHallCode(); File fDir1 = new File(new SDCardUtil(SomeMuseumActivity.this).getMuseums()+File.separator+ lrMuseum.getMuseumHashCode()+File.separator+"themehall"+File.separator+ tHashCode+File.separator+"themehallpic"); if (fDir1!=null && fDir1.exists()) { String picPath = SDCardUtil.getPicAbsolutePath(fDir1, tHashCode); if (picPath!=null && !"".equals(picPath)) { Bitmap bitmap = SDCardUtil.getBitmapFromSD(picPath); if (bitmap!=null) { r1.setBackground(new BitmapDrawable(bitmap)); } } } r1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent in = new Intent(SomeMuseumActivity.this, ThemehallIntroductionActivity.class); in.putExtra("tHashCode", listThemehall.get(item-1).getThemeHallCode()); startActivity(in); } }); tv1.setText(listThemehall.get(item-1).getThemeHallName()); } if (listThemehall.get(item)!=null) { String tHashCode = listThemehall.get(item).getThemeHallCode(); File fDir2 = new File(new SDCardUtil(SomeMuseumActivity.this).getMuseums()+File.separator+ lrMuseum.getMuseumHashCode()+File.separator+"themehall"+File.separator+ tHashCode+File.separator+"themehallpic"); if (fDir2!=null && fDir2.exists()) { String picPath = SDCardUtil.getPicAbsolutePath(fDir2, tHashCode); if (picPath!=null && !"".equals(picPath)) { Bitmap bitmap = SDCardUtil.getBitmapFromSD(picPath); if (bitmap!=null) { r1.setBackground(new BitmapDrawable(bitmap)); } } } r2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent in = new Intent(SomeMuseumActivity.this, ThemehallIntroductionActivity.class); in.putExtra("tHashCode", listThemehall.get(item).getThemeHallCode()); in.putExtra("mHashCode", lrMuseum.getMuseumHashCode()); startActivity(in); } }); tv2.setText(listThemehall.get(item).getThemeHallName()); } lineDynamicThemehall.addView(view); } @SuppressLint("NewApi") private void initTemehallViewSingle(LayoutInflater mInflater,final int item){ View view = mInflater.inflate(R.layout.apart_dynamic_load_themehall, null); RelativeLayout r1 = (RelativeLayout) view.findViewById(R.id.inreoduce_themehall1); RelativeLayout r2 = (RelativeLayout) view.findViewById(R.id.inreoduce_themehall2); TextView tv1 = (TextView) view.findViewById(R.id.tv_themehall_name1); r2.setVisibility(View.INVISIBLE); File fDir = new File(new SDCardUtil(SomeMuseumActivity.this).getMuseums()+File.separator+lrMuseum.getMuseumHashCode()+File.separator+"themehall"+File.separator+listThemehall.get(item).getThemeHallCode()+File.separator+"themehallpic"); if (fDir!=null && fDir.exists()) { String picPath = SDCardUtil.getPicAbsolutePath(fDir, listThemehall.get(item).getThemeHallCode()); if (picPath!=null && !"".equals(picPath)) { Bitmap bitmap = SDCardUtil.getBitmapFromSD(picPath); if (bitmap!=null) { r1.setBackground(new BitmapDrawable(bitmap)); } } } tv1.setText(listThemehall.get(item).getThemeHallName()); r1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent in = new Intent(SomeMuseumActivity.this, ThemehallIntroductionActivity.class); in.putExtra("tHashCode", listThemehall.get(item).getThemeHallCode()); in.putExtra("mHashCode", lrMuseum.getMuseumHashCode()); startActivity(in); } }); lineDynamicThemehall.addView(view); }
这样就可以动态添加View了,还可以点击动态添加的View。
- Android 动态添加View
- Android 动态添加View
- android动态添加view
- Android 动态添加view
- android动态添加View
- Android 动态 添加 View
- android 中动态添加view
- Android 动态添加View布局
- android 动态添加View 两种方法
- Android 动态添加View并设置padding
- 动态添加view
- scroolview 动态添加view
- 动态添加view
- 动态添加View
- LinearLayout动态添加View
- java动态添加view
- 动态添加 view
- Android中动态添加view,删除view,获取view的内容
- Mac OS X 下捕获App局域网通讯包
- SDUT 2162-The Android University ACM Team Selection Contest(模拟)
- 开闭原则(open-close principle)
- 第四周项目一三角形类的构造函数-1
- mac中quick-cocos2dx-2.2.5+sublime text 2搭建lua的开发环境
- Android 动态添加View
- UVa 1623 - Enter The Dragon(贪心)
- HDU Proud Merchants 最优解+背包
- 经验总结:WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法
- 移动端 去除滚动条
- CodeForces 475D CGCDSSQ RMQ
- eclipse打开一直显示Version 1.6.0_34 of the JVM is not suitable for this product
- 日语学习之沪江N3基础 20150401
- C++new出来的和直接声明的对象变量有什么不同呢?