- CompoundButton 源码分析
- LinearLayout 源码分析
- SearchView 源码解析
- LruCache 源码解析
- ViewDragHelper 源码解析
- BottomSheets 源码解析
- Media Player 源码分析
- NavigationView 源码解析
- Service 源码解析
- Binder 源码分析
- Android 应用 Preference 相关及源码浅析 SharePreferences 篇
- ScrollView 源码解析
- Handler 源码解析
- NestedScrollView 源码解析
- SQLiteOpenHelper/SQLiteDatabase/Cursor 源码解析
- Bundle 源码解析
- LocalBroadcastManager 源码解析
- Toast 源码解析
- TextInputLayout
- LayoutInflater 和 LayoutInflaterCompat 源码解析
- TextView 源码解析
- NestedScrolling 事件机制源码解析
- ViewGroup 源码解析
- StaticLayout 源码分析
- AtomicFile 源码解析
- AtomicFile 源码解析
- Spannable 源码分析
- Notification 之 Android 5.0 实现原理
- CoordinatorLayout 源码分析
- Scroller 源码解析
- SwipeRefreshLayout 源码分析
- FloatingActionButton 源码解析
- AsyncTask 源码分析
- TabLayout 源码解析
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
5.2 createViewFromTag 方法解析
createViewFromTag 方法主要通过要实例化 View 的父 View、要实例化 View 的名称、Context 上下文、属性值来实例化 View。该方法主要做的操作有:
- 先尝试用用户设置的 Factory 以及自己私有的 Factory 来实例化 View。
- 如果这几个 Factory 都没有实例化 View,则调用
onCreateView或者createView来实例化 View。
代码分析:
/*
* 缺省方法可见性,好让 BridgeInflater 能重写它。
* 根据父 View、View 名称、Context、属性实例化 View。
*/
private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {
return createViewFromTag(parent, name, context, attrs, false);
}
View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
boolean ignoreThemeAttr) {
// 如果是 View 标签,则用 class 指向的类的完整名称来替换当前名称。(我们都知道,用 Fragment 的时候,可指定 class="Fragment 完整路径名",其他 widget 控件也类似)
if (name.equals("view")) {
name = attrs.getAttributeValue(null, "class");
}
// 应用主题包装,如果允许并且已经被指定
// Apply a theme wrapper, if allowed and one is specified.
if (!ignoreThemeAttr) {
// 获取 Context 中主题属性
final TypedArray ta = context.obtainStyledAttributes(attrs, ATTRS_THEME);
final int themeResId = ta.getResourceId(0, 0);
// 如果包含了主题,则用 ContextThemeWrapper 包装一下
if (themeResId != 0) {
context = new ContextThemeWrapper(context, themeResId);
}
ta.recycle();
}
// 如果根标签是“1995”,则创建一个“BlinkLayout”(其实就是一个 FrameLayout)
// ps:这个没见过在哪里用到过。
if (name.equals(TAG_1995)) {
// Let's party like it's 1995!
return new BlinkLayout(context, attrs);
}
try {
View view;
// 尝试通过 mFactory2 或者 mFactory 来创建 View,这两个是通过 setFactory 和 setFactory2 来设置的。
if (mFactory2 != null) {
view = mFactory2.onCreateView(parent, name, context, attrs);
} else if (mFactory != null) {
view = mFactory.onCreateView(name, context, attrs);
} else {
view = null;
}
// 如果没有设置自定义工厂并且 LayoutInflater 本身私有的 View 工厂不为空,则用私有 View 工厂创建 View。
if (view == null && mPrivateFactory != null) {
view = mPrivateFactory.onCreateView(parent, name, context, attrs);
}
// 如果 View 还为空,也即没有工厂,或者工厂未能正确创建 View,则尝试通过自身的方法实例化 View
if (view == null) {
final Object lastContext = mConstructorArgs[0];
mConstructorArgs[0] = context;
try {
if (-1 == name.indexOf('.')) {
// 如果 View 标签中没有".",则代表是系统的 widget,则调用 onCreateView,这个方法会通过"createView"方法创建 View
// 不过前缀字段会自动补"android.view."。
view = onCreateView(parent, name, attrs);
} else {
// 非系统控件,则 name 本身就是控件的完整路径名。
//通过 widget 完整路径名以及属性创建 View。
view = createView(name, null, attrs);
}
} finally {
mConstructorArgs[0] = lastContext;
}
}
return view;
} catch (InflateException e) {
throw e;
} catch (ClassNotFoundException e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name);
ie.initCause(e);
throw ie;
} catch (Exception e) {
final InflateException ie = new InflateException(attrs.getPositionDescription()
+ ": Error inflating class " + name);
ie.initCause(e);
throw ie;
}
}绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论