Android之 ImageView中setId()的作用
来源:互联网 发布:小斗士火影版mac版 编辑:程序博客网 时间:2024/04/29 15:52
通过代码生成ImageView,并把它添加到布局中来时,可能会遇到setId()方法,那么它有什么作用?
作用如下:
通过代码添加ImageView、TextView等控件时,有时候会用到RelativeLayout.LayoutParams等布局的addRule()方法,如下代码:
ImageView imageView = new ImageView(this.getActivity());imageView.setId(View.generateViewId());...params.addRule(RelativeLayout.BELOW, imageView.getId());
此时就能知道setId()的作用了。就是在某控件的下方、上方等用到具体View的地方,需要getId().
另附上一块完整的代码,但是没setId(),ImageView没有ID,直接取会造成错误。
RelativeLayout layout = new RelativeLayout(this.getActivity()); layout.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); layout.setBackgroundColor(Color.parseColor("#CCCDCDCD")); ImageView imageView = new ImageView(this.getActivity()); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.CENTER_IN_PARENT); imageView.setLayoutParams(params); imageView.setBackgroundResource(R.drawable.create_template); AnimationDrawable frameAnimation = (AnimationDrawable) imageView.getBackground(); if (frameAnimation != null) { frameAnimation.start(); } TextView textView = new TextView(this.getActivity()); params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.BELOW, imageView.getId()); textView.setLayoutParams(params); textView.setText("Generating information..."); layout.addView(imageView); layout.addView(textView); return layout;
0 0
- Android之 ImageView中setId()的作用
- Android中View.setId()
- Android 如何解决代码中view的setId红线问题
- android中ImageView的adjustViewBounds属性的作用
- android 中View.setID,与getID
- android ImageView android:adjustViewBounds属性的作用。
- android ImageView android:adjustViewBounds属性的作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- Android ImageView 的 scaleType属性作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- ImageView的属性android:scaleType作用
- Face Alignment at 3000 FPS 学习理解和具体实现
- 朴素贝叶斯
- C++基础系列(4)
- android实现火箭升空清理内存
- Tarjan——求有向图的强连通分量
- Android之 ImageView中setId()的作用
- linux多窗口screen基本命令
- 关于error:Cannot assign to 'self' outside of a method in the init family
- android 触屏事件总结
- 20150414数据训练营
- 04-SQLite3之运算符and和or
- 阅读程序(5)
- 双缓冲
- 将系统mount 成读写权限