- 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 源码解析
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
2 获取 LayoutInflater 的三种方式:
LayoutInflater inflater = getLayoutInflater();//调用 Activity 的 getLayoutInflater()LayoutInflater inflater = LayoutInflater.from(context);LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
但是,这三种方式本质上还是一样的:
1. getLayoutInflater() 调用的还是 Activity 中的方法:
public LayoutInflater getLayoutInflater() {
return getWindow().getLayoutInflater();
}其中 Window 是一个抽象类,它 唯一 实现类是 PhoneWindow 。
com/android/internal/policyPhoneWindow.java
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
/**
* Return a LayoutInflater instance that can be used to inflate XML view layout
* resources for use in this Window.
*
* @return LayoutInflater The shared LayoutInflater.
*/
@Override
public LayoutInflater getLayoutInflater() {
return mLayoutInflater;
}可以看到,通过· Activity 的 getLayoutInflater() 最终调用的还是第二种方法。
2.通过 LayoutInflater.from(context); 获取 LayoutInflater 对象。
android/view/LayoutInflater.java
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
//通过获取系统服务的方式获取到 LayoutInflater 实例对象
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}以上两种方法,最终还是调用了 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) 获取 LayoutInflater 实例对象。
最终:不管以什么样的方式获取到 LayoutInflater 对象,最终都是 通过系统服务来获取实例对象 。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论