您的位置:首页 > 移动开发 > Android开发

android开发之动态给HorizontalScrollView添加被包含控件

2015-03-04 10:53 597 查看
这个标题起的有点意思,有些小伙伴也许看到这样的标题也是一愣一愣的,那么,我就给大伙解释解释一下。什么叫包含?包含,顾名思义包含者与被包含者没有直接关系,没有继承关系,不然那得叫继承。说到这里,我要给大伙展示的效果是什么呢?请看图:

这是还未包含任何控件:



这是已包含控件:



动态加载而且还不一样的被包含者,那怎么办?

第一步:先看看布局代码

<HorizontalScrollView
android:id="@+id/hsv_file"
android:layout_below="@id/tv_off_cache"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/s_23dp"
>
//动态添加被包含控件于此处
<LinearLayout
android:id="@+id/ll_file"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
</LinearLayout>
</HorizontalScrollView>


第二步:动态添加第一个被包含控件

@ViewInject(R.id.ll_file)
LinearLayout ll_file;

//item_total_downing.xml就是第一个被包含的控件布局文件
View rl_total_downing = getLayoutInflater().inflate(R.layout.item_total_downing, null);
TextView iv_downingNum = (TextView) rl_total_downing.findViewById(R.id.iv_downingNum);
TextView tv_downing = (TextView) rl_total_downing.findViewById(R.id.tv_downing);
ll_file.addView(rl_total_downing); //添加第一个被包含控件


在此要扩展一点东西:

@ViewInject(R.id.ll_file)
LinearLayout ll_file;


咋玩的!其实这是注解工具啦,那么,你要使用的话:

1、下载xUtils开源工具jar包:http://download.csdn.net/detail/dg41598/8435075

2、将下载的

3、在Activity类中的onCreate方法中:

setContentView(R.layout.personal_center_layout);
ViewUtils.inject(this);


4、声明类的全局变量范围中:

@ViewInject(R.id.ll_file)
LinearLayout ll_file;


这样就省略了大量的findViewById()!

第三步:动态添加第二、三….个被包含控件

List<CourseItemBean> courseItemBeans = new ArrayList<CourseItemBean>();
private BitmapUtils bitmapUtils = new BitmapUtils(this);;

//动态添加已下载文件到HorizontalScrollView当中
for(int i = 0;i<courseItemBeans.size();i++) {
final CourseItemBean itemEntity = courseItemBeans.get(i);
//horizontalscrollview_list_item.xml就是第二、三...个被包含的控件布局文件
final RelativeLayout item = (RelativeLayout) getLayoutInflater().inflate(R.layout.horizontalscrollview_list_item, null);
ImageView icon = (ImageView) item.findViewById(R.id.icon); //设置已下载文件的图片
//压缩图片
bitmapUtils.display(icon,itemEntity.getImg());
TextView tv_downing = (TextView) item.findViewById(R.id.tv_downing);
tv_downing.setText(itemEntity.getTitle()); //设置已下载文件的标题
//因为第一个下标位置被添加了,因此以后的第二、三...都得在其下标上加1
ll_file.addView(item,i+1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐