Activity 中获取所有控件 并设置自定义字体
来源:互联网 发布:jenny wang 知乎 编辑:程序博客网 时间:2024/06/05 17:18
public class MyActivity extends Activity {
......
//获取当前Activity里所有控件
public List<View> getAllChildViews() {
View view = this.getWindow().getDecorView();
return getAllChildViews(view);
}
//获取指定View里所有控件
public List<View> getAllChildViews(View view) {
List<View> allchildren = new ArrayList<View>();
if (view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild = vp.getChildAt(i);
allchildren.add(viewchild);
allchildren.addAll(getAllChildViews(viewchild));
}
}
return allchildren;
}
}View view = this.getWindow().getDecorView();
return getAllChildViews(view);
}
//获取指定View里所有控件
public List<View> getAllChildViews(View view) {
List<View> allchildren = new ArrayList<View>();
if (view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewchild = vp.getChildAt(i);
allchildren.add(viewchild);
allchildren.addAll(getAllChildViews(viewchild));
}
}
return allchildren;
}
当然,这代码也是有问题的,就是无法得到ListView这类控件的所有Item,应为ListView不是继承ViewGroup ,这是个鸡肋方法,很少用,如果大家有兴趣的话,可以帮忙改进写。
得到当前Activity里的所有控件,有什么用呢,我主要是为了给当前Activity设置自定义字体。
private void setTypeface(Typeface typeface) {
List<View> children = getAllChildViews();
for (View child : children) {
if (child instanceof TextView) {
TextView tv = (TextView) child;
tv.setTypeface(typeface);
}
}
}
List<View> children = getAllChildViews();
for (View child : children) {
if (child instanceof TextView) {
TextView tv = (TextView) child;
tv.setTypeface(typeface);
}
}
}
- Activity 中获取所有控件 并设置自定义字体
- C#中ListBox控件设置Item字体并居中显示
- 在自定义的view中设置activity的控件
- 设置一个对话框的字体,以及所有控件字体
- 自定义控件中setText()设置字体相同大小无法与原生控件一致
- 自定义控件中setText()设置字体相同大小无法与原生控件一致
- linux下gtk2.0编程,如何设置程序中所有控件统一的字体?
- Android获取并设置Activity的亮度
- Android获取并设置Activity的亮度
- Android获取并设置Activity的亮度
- swing 批量设置所有窗口控件的字体特征
- 获取app中所有activity集合
- Android中添加并设置文字字体
- VC获取Excel所有工作表名,并将其显示到一个CListCtrl控件中
- 获取所有工作表名,并将其显示到一个CListCtrl控件中
- vc中控件字体的设置
- MFC控件中字体的设置
- MFC控件中字体的设置
- 项目一任务2
- RSIC设计思想
- SNMP之JRobin Core学习
- asp.net 的给文本框指定字符、(event.keyCode) javascript
- 堆 和 栈的 区别(经典)
- Activity 中获取所有控件 并设置自定义字体
- jquery使用$.getJson()跨域大数据量请求方法
- jQuery入门之二:设置属性,设置事件处理
- C#下的FTP上传和下载
- 云计算理念进阶
- thread dump
- 多数据源的配置
- 适配器模式
- 解密中小企业的留人机制