下面列出了android.view.LayoutInflater#cloneInContext ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Object getSystemService(String name) {
if (LAYOUT_INFLATER_SERVICE.equals(name)) {
if (mLayoutInflater == null) {
// 重写插件Application Context的获取LayoutInflater方法,解决插件使用Application Context
// 无法访问插件资源的问题,原因是LayoutInflater的构造函数使用的是Base Context的outerContext,
// 而这个OuterContext是宿主的Application
LayoutInflater inflater = (LayoutInflater) super.getSystemService(name);
mLayoutInflater = forApp ? inflater.cloneInContext(this) : inflater;
// 设置mPrivateFactory,修复多个插件同时依赖使用同名View的问题,比如android design库
LayoutInflaterCompat.setPrivateFactory(inflater);
}
return mLayoutInflater;
}
return super.getSystemService(name);
}
@Nullable
@Override
public View onCreateView(
LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
this.wrappedContext = new ContextThemeWrapper(getContext(), getShapeTheme());
LayoutInflater layoutInflaterWithThemedContext =
layoutInflater.cloneInContext(wrappedContext);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getActivity().getWindow();
statusBarColor = window.getStatusBarColor();
final TypedValue value = new TypedValue();
wrappedContext
.getTheme()
.resolveAttribute(R.attr.colorPrimaryDark, value, true);
window.setStatusBarColor(value.data);
}
return super.onCreateView(layoutInflaterWithThemedContext, viewGroup, bundle);
}
public static void owlBeforeCreate(Activity activity){
Window window = activity.getWindow();
LayoutInflater layoutInflater = window.getLayoutInflater();
// replace the inflater in window
LayoutInflater injectLayoutInflater1 = Factory4InjectedInflater.newInstance(layoutInflater, activity);
injectLayoutInflater(injectLayoutInflater1
, activity.getWindow()
, activity.getWindow().getClass()
, WINDOW_INFLATER);
// replace the inflater in current ContextThemeWrapper
LayoutInflater injectLayoutInflater2 = injectLayoutInflater1.cloneInContext(activity);
injectLayoutInflater(injectLayoutInflater2
, activity
, ContextThemeWrapper.class
, THEME_INFLATER);
// insert owlViewContext into root view.
View v = activity.getWindow().getDecorView();
OwlViewContext owlObservable = new OwlViewContext();
insertViewContext(v, owlObservable);
}
@Override
public Object getSystemService(@NonNull String name) {
if (LAYOUT_INFLATER_SERVICE.equals(name)) {
LayoutInflater layoutInflater = (LayoutInflater) mPluginContext.getSystemService(name);
return layoutInflater.cloneInContext(this);
}
return mOriginActivity.getSystemService(name);
}
@SuppressLint("RestrictedApi")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
context = getContext();
activity = getActivity();
if (fragmentLayout() != 0) {
final Context contextThemeWrapper = new ContextThemeWrapper(getContext(), getContext().getTheme());
LayoutInflater themeAwareInflater = inflater.cloneInContext(contextThemeWrapper);
View view = themeAwareInflater.inflate(fragmentLayout(), container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
return super.onCreateView(inflater, container, savedInstanceState);
}
private void initLayoutInflater() {
LayoutInflater layoutInflater = ((LayoutInflater) mContentProxy.getContext().getSystemService(Context
.LAYOUT_INFLATER_SERVICE));
if (null != layoutInflater) {
mLayoutInflater = layoutInflater.cloneInContext(this);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mView == null) {
boolean isLight = Utils.getSpUtils().getString("loginmode").equals("phone");
Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), isLight ? R.style.ThemeLight : R.style.ThemeDark);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
mView = localInflater.inflate(getContentViewLayoutID(), null);
}
ViewGroup parent = (ViewGroup) mView.getParent();
if (parent != null) {
parent.removeView(mView);
}
return mView;
}
public static Object getWrappedSystemService(Object service,
String name,
boolean wrapLayoutInflater) {
if (Context.WINDOW_SERVICE.equals(name)) {
boolean goAhead=true;
for (StackTraceElement entry : Thread.currentThread().getStackTrace()) {
try {
Class cls=Class.forName(entry.getClassName());
if (Dialog.class.isAssignableFrom(cls)) {
goAhead=false;
break;
}
}
catch (ClassNotFoundException e) {
// ???
}
}
if (goAhead) {
service=new SecureWindowManagerWrapper((WindowManager)service);
}
}
else if (Context.LAYOUT_INFLATER_SERVICE.equals(name) && wrapLayoutInflater) {
LayoutInflater original=(LayoutInflater)service;
Context securified=
new SecureContextWrapper(original.getContext(), true, true);
service=original.cloneInContext(securified);
}
return(service);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mTheme > 0) {
Context newContext = new ContextThemeWrapper(inflater.getContext(), mTheme);
inflater = inflater.cloneInContext(newContext);
}
mInflater = inflater;
View view = inflater.inflate(R.layout.mnd_list_compact, container, false);
mListView = (ListView) view.findViewById(R.id.mnd_list_compact);
return view;
}
/**
* 配置LAYOUT_INFLATER_SERVICE时的一些参数
*
* @param context 调用着的context
* @param systemServcie systemServer对象
* @param name server的名字
* @return systemServer对象
*/
public static Object getSystemService(Context context, Object systemServcie, String name) {
if (Context.LAYOUT_INFLATER_SERVICE.equals(name)) {
LayoutInflater inflater = (LayoutInflater) systemServcie;
inflater.cloneInContext(context);
//使用某些加固之后该inflater里的mContext变量一直是系统的context,根本不是当前Context
//所以这里手动设置一次
PluginUtil.setField(inflater, "mContext", context);
return inflater;
}
return systemServcie;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if (mTheme > 0) {
Context newContext = new ContextThemeWrapper(inflater.getContext(), mTheme);
inflater = inflater.cloneInContext(newContext);
}
mInflater = inflater;
mView = inflater.inflate(R.layout.mnd_list, container, false);
mListView = (ListView) mView.findViewById(R.id.mnd_list);
mPinnedContainer = (ViewGroup) mView.findViewById(R.id.mnd_section_pinned);
mPinnedDivider = mView.findViewById(R.id.mnd_divider_pinned);
return mView;
}
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
Context contextThemeWrapper = new ContextThemeWrapper(context, theme);
return inflater.cloneInContext(contextThemeWrapper);
}
/**
* Inflate and return the default layout requested by AppWidget provider.
*/
protected View getDefaultView() {
if (LOGD) {
Log.d(TAG, "getDefaultView");
}
View defaultView = null;
Exception exception = null;
try {
if (mInfo != null) {
Context theirContext = getRemoteContext();
mRemoteContext = theirContext;
LayoutInflater inflater = (LayoutInflater)
theirContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = inflater.cloneInContext(theirContext);
inflater.setFilter(INFLATER_FILTER);
AppWidgetManager manager = AppWidgetManager.getInstance(mContext);
Bundle options = manager.getAppWidgetOptions(mAppWidgetId);
int layoutId = mInfo.initialLayout;
if (options.containsKey(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY)) {
int category = options.getInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY);
if (category == AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD) {
int kgLayoutId = mInfo.initialKeyguardLayout;
// If a default keyguard layout is not specified, use the standard
// default layout.
layoutId = kgLayoutId == 0 ? layoutId : kgLayoutId;
}
}
defaultView = inflater.inflate(layoutId, this, false);
} else {
Log.w(TAG, "can't inflate defaultView because mInfo is missing");
}
} catch (RuntimeException e) {
exception = e;
}
if (exception != null) {
Log.w(TAG, "Error inflating AppWidget " + mInfo + ": " + exception.toString());
}
if (defaultView == null) {
if (LOGD) Log.d(TAG, "getDefaultView couldn't find any view, so inflating error");
defaultView = getErrorView();
}
return defaultView;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
return super.onCreateView(localInflater, container, savedInstanceState);
}
/**
* Create and return the user interface view for this fragment.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
int theme = getArguments().getInt("theme");
int initialHour = getArguments().getInt("hour");
int initialMinute = getArguments().getInt("minute");
boolean isClientSpecified24HourTime = getArguments().getBoolean("isClientSpecified24HourTime");
boolean is24HourTime = getArguments().getBoolean("is24HourTime");
// Unless we inflate using a cloned inflater with a Holo theme,
// on Lollipop devices the TimePicker will be the new-style
// radial TimePicker, which is not what we want. So we will
// clone the inflater that we're given but with our specified
// theme, then inflate the layout with this new inflater.
Context contextThemeWrapper = new ContextThemeWrapper(
getActivity(),
theme == SlideDayTimePicker.HOLO_DARK ?
android.R.style.Theme_Holo : android.R.style.Theme_Holo_Light);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View v = localInflater.inflate(R.layout.fragment_time, container, false);
mTimePicker = (TimePicker) v.findViewById(R.id.timePicker);
// block keyboard popping up on touch
mTimePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
mTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute)
{
mCallback.onTimeChanged(hourOfDay, minute);
}
});
// If the client specifies a 24-hour time format, set it on
// the TimePicker.
if (isClientSpecified24HourTime)
{
mTimePicker.setIs24HourView(is24HourTime);
}
else
{
// If the client does not specify a 24-hour time format, use the
// device default.
mTimePicker.setIs24HourView(DateFormat.is24HourFormat(
getTargetFragment().getActivity()));
}
mTimePicker.setCurrentHour(initialHour);
mTimePicker.setCurrentMinute(initialMinute);
// Fix for the bug where a TimePicker's onTimeChanged() is not called when
// the user toggles the AM/PM button. Only applies to 4.0.0 and 4.0.3.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH &&
Build.VERSION.SDK_INT <= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
{
fixTimePickerBug18982();
}
return v;
}
/**
* Create and return the user interface view for this fragment.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
int initialDay = getArguments().getInt("initialDay");
boolean isCustomDaysArraySpecified = getArguments().getBoolean("isCustomDaysArraySpecified");
String[] daysArray;
if (isCustomDaysArraySpecified)
{
daysArray = getArguments().getStringArray("customDaysArray");
}
else
{
daysArray = getResources().getStringArray(R.array.days_array);
}
// Unless we inflate using a cloned inflater with a Holo theme,
// on Lollipop devices the TimePicker will be the new-style
// radial TimePicker, which is not what we want. So we will
// clone the inflater that we're given but with our specified
// theme, then inflate the layout with this new inflater.
int theme = getArguments().getInt("theme");
Context contextThemeWrapper = new ContextThemeWrapper(
getActivity(),
theme == SlideDayTimePicker.HOLO_DARK ?
android.R.style.Theme_Holo : android.R.style.Theme_Holo_Light);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View v = localInflater.inflate(R.layout.fragment_day, container, false);
CustomNumberPicker dayPicker = (CustomNumberPicker) v.findViewById(R.id.dayPicker);
// remove blinking cursor from NumberPicker
enableNumberPickerEditing(dayPicker, false);
// block keyboard popping up on touch
dayPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
dayPicker.setMinValue(0);
dayPicker.setMaxValue(daysArray.length - 1);
dayPicker.setDisplayedValues(daysArray);
dayPicker.setValue(initialDay);
dayPicker.setOnValueChangedListener(new OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal)
{
mCallback.onDayChanged(newVal);
}
});
return v;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.AppTheme);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
return super.onCreateView(localInflater, container, savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TypedArray a = mStyledContext.obtainStyledAttributes(null,
R.styleable.PreferenceFragment,
R.attr.preferenceFragmentStyle,
0);
mLayoutResId = a.getResourceId(R.styleable.PreferenceFragment_android_layout,
mLayoutResId);
final Drawable divider = a.getDrawable(
R.styleable.PreferenceFragment_android_divider);
final int dividerHeight = a.getDimensionPixelSize(
R.styleable.PreferenceFragment_android_dividerHeight, -1);
final boolean allowDividerAfterLastItem = a.getBoolean(
R.styleable.PreferenceFragment_allowDividerAfterLastItem, false);
a.recycle();
// Need to theme the inflater to pick up the preferenceFragmentListStyle
final TypedValue tv = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, tv, true);
final int theme = tv.resourceId;
final Context themedContext = new ContextThemeWrapper(inflater.getContext(), theme);
final LayoutInflater themedInflater = inflater.cloneInContext(themedContext);
final View view = themedInflater.inflate(mLayoutResId, container, false);
final View rawListContainer = view.findViewById(R.id.list_container);
if (!(rawListContainer instanceof ViewGroup)) {
throw new RuntimeException("Content has view with id attribute "
+ "'R.id.list_container' that is not a ViewGroup class");
}
final ViewGroup listContainer = (ViewGroup) rawListContainer;
final RecyclerView listView = onCreateRecyclerView(themedInflater, listContainer,
savedInstanceState);
if (listView == null) {
throw new RuntimeException("Could not create RecyclerView");
}
mList = listView;
mDividerDecoration = onCreateItemDecoration();
if (mDividerDecoration != null) {
mList.addItemDecoration(mDividerDecoration);
}
setDivider(divider);
if (dividerHeight != -1) {
setDividerHeight(dividerHeight);
}
mDividerDecoration.setAllowDividerAfterLastItem(allowDividerAfterLastItem);
listContainer.addView(mList);
mHandler.post(mRequestFocus);
return view;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this.getActivity(), this.getArguments().getInt("com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_THEME_RES"));
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
return localInflater.inflate(this.getArguments().getInt("com.heinrichreimersoftware.materialintro.SimpleFragment.ARGUMENT_LAYOUT_RES"), container, false);
}
/**
* Create and return the user interface view for this fragment.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
int theme = getArguments().getInt("theme");
int initialYear = getArguments().getInt("year");
int initialMonth = getArguments().getInt("month");
int initialDay = getArguments().getInt("day");
Date minDate = (Date) getArguments().getSerializable("minDate");
Date maxDate = (Date) getArguments().getSerializable("maxDate");
// Unless we inflate using a cloned inflater with a Holo theme,
// on Lollipop devices the DatePicker will be the new-style
// DatePicker, which is not what we want. So we will
// clone the inflater that we're given but with our specified
// theme, then inflate the layout with this new inflater.
Context contextThemeWrapper = new ContextThemeWrapper(
getActivity(),
theme == SlideDateTimePicker.HOLO_DARK ?
android.R.style.Theme_Holo :
android.R.style.Theme_Holo_Light);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
View v = localInflater.inflate(R.layout.fragment_date, container, false);
mDatePicker = (CustomDatePicker) v.findViewById(R.id.datePicker);
// block keyboard popping up on touch
mDatePicker.setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);
mDatePicker.init(
initialYear,
initialMonth,
initialDay,
new OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
mCallback.onDateChanged(year, monthOfYear, dayOfMonth);
}
});
if (minDate != null)
mDatePicker.setMinDate(minDate.getTime());
if (maxDate != null)
mDatePicker.setMaxDate(maxDate.getTime());
return v;
}