下面列出了怎么用android.support.v4.view.LayoutInflaterCompat的API类实例代码及写法,或者点击链接到github查看源代码。
public static void installPandroidViewFactory(AppCompatActivity compatActivity) {
List<LayoutInflater.Factory2> factories = new ArrayList<>();
if (compatActivity instanceof PandroidFactoryProvider) {
addProviderFactories((PandroidFactoryProvider) compatActivity, factories);
}
if (compatActivity.getApplication() instanceof PandroidFactoryProvider) {
addProviderFactories((PandroidFactoryProvider) compatActivity.getApplication(), factories);
}
if (!factories.isEmpty()) {
LayoutInflater inflater = LayoutInflater.from(compatActivity);
if (inflater.getFactory2() == null) {
PandroidViewFactory factory = new PandroidViewFactory(compatActivity.getDelegate(), factories);
LayoutInflaterCompat.setFactory2(inflater, factory);
} else {
LogcatLogger.getInstance().w(TAG, "can't set layout inflater factory");
}
} else {
LogcatLogger.getInstance().w(TAG, "Your activity or application should implement PandroidFactoryProvider to install PandroidLayoutInflaterFactory");
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
super.onCreate(savedInstanceState);
setContentView(R.layout.lo_fire_auth_home);
mImgvwProfile = (ImageView) findViewById(R.id.imgvw_fireauth_home_profile);
mTvDisplayName = (TextView) findViewById(R.id.tv_fireauth_home_displayname);
mTvEmail = (TextView) findViewById(R.id.tv_fireauth_home_email);
mFireAuth = FirebaseAuth.getInstance();
FirebaseUser fireUser = mFireAuth.getCurrentUser();
if (fireUser != null) {
Picasso.with(this)
.load(fireUser.getPhotoUrl())
.placeholder(R.drawable.img_firebase_logo)
.into(mImgvwProfile);
if(!TextUtils.isEmpty(fireUser.getDisplayName()))
mTvDisplayName.setText(fireUser.getDisplayName());
else mTvDisplayName.setVisibility(View.GONE);
mTvEmail.setText(fireUser.getEmail());
}
}
public static void setCustomTheme(Context context, SparseIntArray customAttrs) {
if (customAttrs == null || customAttrs.size() == 0) {
currentAttrs = null;
return;
}
TypedValue tmp = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.textColorPrimary, tmp, true);
int textColorPrimaryOriginal = (tmp.type >= TypedValue.TYPE_FIRST_COLOR_INT && tmp.type <= TypedValue.TYPE_LAST_COLOR_INT) ?
tmp.data : Color.TRANSPARENT;
int textColorPrimaryOverridden = customAttrs.get(android.R.attr.textColorPrimary, textColorPrimaryOriginal);
try {
processWindow(context, customAttrs, textColorPrimaryOriginal, textColorPrimaryOverridden);
} catch (Exception e) {
Logger.e(TAG, e);
}
CustomThemeHelper instance = new CustomThemeHelper(context, customAttrs, textColorPrimaryOriginal, textColorPrimaryOverridden);
LayoutInflaterCompat.setFactory(instance.inflater, instance);
currentAttrs = customAttrs;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (needAnimator()) {
AnimatorManager.setConfig(new AnimatorConfig.Builder()
.textviewVisibleAnimationType(ViewAnimatorType.AlphaHideAnimator)
.textviewTextAnimationType(ViewAnimatorType.AlphaUpdateAnimator)
.imageviewVisibleAnimationType(ViewAnimatorType.AlphaHideAnimator)
.build());
}
LayoutInflaterCompat.setFactory(getLayoutInflater(), getSkinDelegate());
super.onCreate(savedInstanceState);
}
private void hookLayoutInflater(Context context) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
try {
Field field = LayoutInflater.class.getDeclaredField("mFactorySet");
field.setAccessible(true);
field.setBoolean(layoutInflater, false);
LayoutInflaterCompat.setFactory2(layoutInflater, AndroidSkinFactory.from(context,layoutInflater));
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
private void installLayoutFactory(Context context) {
try {
LayoutInflater layoutInflater = LayoutInflater.from(context);
LayoutInflaterCompat.setFactory(layoutInflater, getSkinDelegate(context));
} catch (Exception e) {
Slog.i("SkinActivity", "A factory has already been set on this LayoutInflater");
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory(getLayoutInflater(), getSkinDelegate());
super.onCreate(savedInstanceState);
updateStatusBarColor();
updateWindowBackground();
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//在setContentView之前设置好工厂
LayoutInflater layoutInflater = LayoutInflater.from(this);
LayoutInflaterCompat.setFactory(layoutInflater, this);
SkinManager.getInstance().init(this);
/*if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory(layoutInflater, this);
}*/
super.onCreate(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindView();
initView();
setupComponent();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
super.onCreate(savedInstanceState);
ThemeUtil.setCustomTheme(this);
LanguageUtil.updateLanguage(this);
tintBars();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// define the IconicsLayoutInflater
// this is compatible with calligraphy and other libs which wrap the baseContext
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
super.onCreate(savedInstanceState);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
//在setContentView之前设置好工厂
LayoutInflater layoutInflater = LayoutInflater.from(this);
LayoutInflaterCompat.setFactory(layoutInflater, this);
SkinManager.getInstance().init(this);
/*if (layoutInflater.getFactory() == null) {
LayoutInflaterCompat.setFactory(layoutInflater, this);
}*/
super.onCreate(savedInstanceState);
}
protected void onCreate(@Nullable Bundle savedInstanceState)
{
LayoutInflater layoutInflater = LayoutInflater.from(this);
LayoutInflaterCompat.setFactory(layoutInflater, this);
super.onCreate(savedInstanceState);
SkinManager.getInstance().addChangedListener(this);
}
public static void attach(@NonNull Activity activity) {
if (activity instanceof AppCompatActivity) {
LayoutInflaterCompat.setFactory(activity.getLayoutInflater(),
new RobotoInflater(((AppCompatActivity) activity).getDelegate(), activity.getWindow()));
} else {
final Window window = activity.getWindow();
final Window.Callback callback = window.getCallback();
LayoutInflaterCompat.setFactory(activity.getLayoutInflater(),
new RobotoInflater(AppCompatDelegate.create(activity, StubAppCompatCallback.INSTANCE), window));
window.setCallback(callback);
}
}
/**
* 初始化夜间控制器
* @param activity 上下文
* @return
*/
public ChangeModeController init(final Activity activity,final Class mClass){
init();
LayoutInflaterCompat.setFactory(LayoutInflater.from(activity), new LayoutInflaterFactory() {
@Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
View view = null;
try {
if(name.indexOf('.') == -1){
if ("View".equals(name)) {
view = LayoutInflater.from(context).createView(name, "android.view.", attrs);
}
if (view == null) {
view = LayoutInflater.from(context).createView(name, "android.widget.", attrs);
}
if (view == null) {
view = LayoutInflater.from(context).createView(name, "android.webkit.", attrs);
}
}else{
if (view == null){
view = LayoutInflater.from(context).createView(name, null, attrs);
}
}
if(view != null){
// Log.e("TAG", "name = " + name);
for (int i = 0; i < attrs.getAttributeCount(); i++) {
// Log.e("TAG", attrs.getAttributeName(i) + " , " + attrs.getAttributeValue(i));
if (attrs.getAttributeName(i).equals(ATTR_BACKGROUND)) {
mBackGroundViews.add(new AttrEntity<View>(view,getAttr(mClass,attrs.getAttributeValue(i))));
}
if (attrs.getAttributeName(i).equals(ATTR_TEXTCOLOR)) {
mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i))));
}
if (attrs.getAttributeName(i).equals(ATTR_TWO_TEXTCOLOR)) {
mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i))));
}
if (attrs.getAttributeName(i).equals(ATTR_THREE_TEXTCOLOR)) {
mOneTextColorViews.add(new AttrEntity<TextView>((TextView)view,getAttr(mClass,attrs.getAttributeValue(i))));
}
if (attrs.getAttributeName(i).equals(ATTR_BACKGROUND_DRAWABLE)) {
mBackGroundDrawableViews.add(new AttrEntity<View>(view,getAttr(mClass,attrs.getAttributeValue(i))));
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return view;
}
});
return this;
}
public static LayoutInflater getLayoutInflater(AppCompatActivity activity) {
LayoutInflater inflater = LayoutInflater.from(activity).cloneInContext(activity);
LayoutInflaterCompat.setFactory2(inflater, new WidgetInflaterFactory());
return inflater;
}
public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
LayoutInflater result = this.mHost.onGetLayoutInflater();
getChildFragmentManager();
LayoutInflaterCompat.setFactory(result, this.mChildFragmentManager.getLayoutInflaterFactory());
return result;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
LayoutInflaterCompat.setFactory(getLayoutInflater(), new IconicsLayoutInflater(getDelegate()));
FacebookSdk.sdkInitialize(getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();
super.onCreate(savedInstanceState);
setContentView(R.layout.lo_fire_signin);
mCrdntrlyot = (CoordinatorLayout) findViewById(R.id.cordntrlyot_fireauth);
mTxtinptlyotEmail = (TextInputLayout) findViewById(R.id.txtinputlyot_fireauth_email);
mTxtinptlyotPaswrd = (TextInputLayout) findViewById(R.id.txtinputlyot_fireauth_password);
mTxtinptEtEmail = (TextInputEditText) findViewById(R.id.txtinptet_fireauth_email);
mTxtinptEtPaswrd = (TextInputEditText) findViewById(R.id.txtinptet_fireauth_password);
mAppcmptbtnSignup = (AppCompatButton) findViewById(R.id.appcmptbtn_fireauth_signin);
mTvFrgtPaswrd = (TextView) findViewById(R.id.tv_fireauth_frgtpaswrd);
mImgvwFirebase = (ImageView) findViewById(R.id.imgvw_fireauth_firebase);
mImgvwGp = (ImageView) findViewById(R.id.imgvw_fireauth_social_gp);
mImgvwFb = (ImageView) findViewById(R.id.imgvw_fireauth_social_fb);
mPrgrsbrMain = (ProgressBar) findViewById(R.id.prgrsbr_fireauth);
mFireAuth = FirebaseAuth.getInstance();
mFireDB = FirebaseDatabase.getInstance();
FirebaseAuth.getInstance().signOut();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mAppcmptbtnSignup.setOnClickListener(this);
mTvFrgtPaswrd.setOnClickListener(this);
mImgvwFirebase.setOnClickListener(this);
mImgvwGp.setOnClickListener(this);
mImgvwFb.setOnClickListener(this);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
LayoutInflater layoutInflater = LayoutInflater.from(this);
LayoutInflaterCompat.setFactory(layoutInflater, this);
super.onCreate(savedInstanceState);
}
/**
* Call this method in Activity.onCreate and before super.onCreate(...)
*/
public static void init(AppCompatActivity activity) {
LayoutInflaterCompat.setFactory(activity.getLayoutInflater(), new MdLayoutInflaterFactory(activity.getDelegate()));
}
/**
* Installs the factory to the given context prior to API level 21.
* It will use AppCompat's layout inflater to inflate views and
* set a proper Roboto typeface to the view. Roboto fonts are also used
* when user is using a custom font.
*
* @param context A context.
* @see #installViewFactory(Context, boolean)
* @since 0.1.1
* @deprecated
*/
@Deprecated
public static void installViewFactory(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
LayoutInflaterCompat.setFactory(LayoutInflater.from(context),
new TypefaceCompatFactory(context, false));
}
}
/**
* Installs the factory to the given context prior to API level 21.
* It will use AppCompat's layout inflater to inflate views and
* set a proper typeface to the view if needed.
* If typeface detection is enabled the factory automatically detects the used system typeface
* and adjust its behavior properly.
* This makes sure that the newer Roboto typefaces are only used if no custom typefaces are applied by the system.
* <p>
* <b>Note:</b> Typeface detection only works starting with API level 14 and comes with a small performance penalty.
*
* @param context A context.
* @param typefaceDetectionEnabled True if the factory should automatically detect the used system typeface and adjust its behavior properly.
* This makes sure that the newer Roboto typefaces are only used if no custom typefaces are applied by the system.
* @since 0.1.1
* @deprecated
*/
@Deprecated
public static void installViewFactory(Context context, boolean typefaceDetectionEnabled) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
LayoutInflaterCompat.setFactory(LayoutInflater.from(context),
new TypefaceCompatFactory(context, typefaceDetectionEnabled));
}
}