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。
这里写图片描述

0 0
原创粉丝点击