下面列出了android.content.res.Resources#getConfiguration() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Context updateResources(Context baseContext) {
if (G.selectedLanguage == null) {
G.selectedLanguage = Locale.getDefault().getLanguage();
}
Locale locale = new Locale(G.selectedLanguage);
Locale.setDefault(locale);
Resources res = baseContext.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLocale(locale);
} else {
configuration.locale = locale;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
baseContext = baseContext.createConfigurationContext(configuration);
} else {
res.updateConfiguration(configuration, res.getDisplayMetrics());
}
G.context = baseContext;
return baseContext;
}
/**
* 设置语言类型
*/
public static void setApplicationLanguage(Context context) {
Resources resources = context.getApplicationContext().getResources();
DisplayMetrics dm = resources.getDisplayMetrics();
Configuration config = resources.getConfiguration();
Locale locale = getSetLanguageLocale(context);
config.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LocaleList localeList = new LocaleList(locale);
LocaleList.setDefault(localeList);
config.setLocales(localeList);
context.getApplicationContext().createConfigurationContext(config);
Locale.setDefault(locale);
}
resources.updateConfiguration(config, dm);
}
public static ContextWrapper wrap(Context context, Locale newLocale) {
Resources res = context.getResources();
Configuration configuration = res.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale);
LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);
context = context.createConfigurationContext(configuration);
} else {
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);
}
return new ContextWrapper(context);
}
/**
* Retrieve offset introduce by the navigation bar.
*
* @return bottom offset due to navigation bar.
*/
private int getNavigationBarOffset() {
int result = 0;
Resources resources = mHoldingActivity.getResources();
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
&& resources.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
result = resources.getDimensionPixelSize(resourceId);
}
}
return result;
}
/**
* Set the app to use the given locale. Useful for testing translations. This is normally
* not needed because the device locale is applied automatically.
* @param context - context from which to get resources
* @param locale - the locale to use
*/
public static void setLocale(@NonNull Context context, @NonNull Locale locale) {
Locale.setDefault(locale);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = Locale.getDefault();
res.updateConfiguration(conf, dm);
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context) {
Resources resources = context.getResources();
Locale locale = getLocaleByLanguage(context);
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
configuration.setLocales(new LocaleList(locale));
return context.createConfigurationContext(configuration);
}
/**
* Switch the locale of current app to explicitly specified or default one
*
* @param locale null for default
*/
public static boolean switchTo(final Context context, final Locale locale) {
final Resources resources = context.getResources();
final Configuration configuration = resources.getConfiguration();
if (match(locale, configuration.locale)) return false;
setConfigurationLocale(configuration, locale);
resources.updateConfiguration(configuration, null);
return true;
}
private static Context updateResources(Context context, Locale locale) {
Locale.setDefault(locale);
Resources res = context.getResources();
Configuration config = new Configuration(res.getConfiguration());
config.setLocale(locale);
context = context.createConfigurationContext(config);
return context;
}
/**
* 修改系统语言 ( APP 多语言, 单独改变 APP 语言 )
* @param context {@link Context} - Activity
* @param locale {@link Locale}
* @return {@code true} success, {@code false} fail
*/
public static boolean applyLanguage(final Context context, final Locale locale) {
if (context != null && locale != null) {
try {
// 获取 res 资源对象
Resources resources = context.getResources();
// 获取设置对象
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// apply locale
config.setLocale(locale);
context.createConfigurationContext(config);
} else {
// updateConfiguration
// 获取屏幕参数: 主要是分辨率, 像素等
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
config.locale = locale;
// 更新语言
resources.updateConfiguration(config, displayMetrics);
}
return true;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "applyLanguage");
}
}
return false;
}
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
mTranslucentStatusBar = translucentStatusBar;
mTranslucentNavBar = traslucentNavBar;
}
public static void change(Context context, String language) {
if (context != null) {
Resources resources = context.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale(language.toLowerCase());
resources.updateConfiguration(configuration, displayMetrics);
Log.i(LOG_TAG, "⚙ Language is set to: " + language.toUpperCase());
}
}
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
mTranslucentStatusBar = translucentStatusBar;
mTranslucentNavBar = traslucentNavBar;
}
@SuppressWarnings("deprecation")
private static void updateResourcesLegacy(Context context, String language) {
Locale locale = getLocale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
public static Context loadLocale( Context context, String languageTag )
{
if (systemLocale == null) {
systemLocale = Locale.getDefault().getLanguage();
}
Locale customLocale = localeForLanguageTag(languageTag);
Locale.setDefault(customLocale);
Log.i("loadLocale", languageTag);
Resources resources = context.getApplicationContext().getResources();
Configuration config = resources.getConfiguration();
if (Build.VERSION.SDK_INT >= 17)
config.setLocale(customLocale);
else config.locale = customLocale;
if (Build.VERSION.SDK_INT >= 25) {
return new ContextWrapper(context.createConfigurationContext(config));
} else {
DisplayMetrics metrics = resources.getDisplayMetrics();
//noinspection deprecation
resources.updateConfiguration(config, metrics);
return new ContextWrapper(context);
}
}
private SystemBarConfig(Activity activity, boolean translucentStatusBar, boolean traslucentNavBar) {
Resources res = activity.getResources();
mInPortrait = (res.getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
mSmallestWidthDp = getSmallestWidthDp(activity);
mStatusBarHeight = getInternalDimensionSize(res, STATUS_BAR_HEIGHT_RES_NAME);
mActionBarHeight = getActionBarHeight(activity);
mNavigationBarHeight = getNavigationBarHeight(activity);
mNavigationBarWidth = getNavigationBarWidth(activity);
mHasNavigationBar = (mNavigationBarHeight > 0);
mTranslucentStatusBar = translucentStatusBar;
mTranslucentNavBar = traslucentNavBar;
}
public static void initWith(@NonNull final Context context) {
final Resources resources = context.getResources();
final DisplayMetrics dm = resources.getDisplayMetrics();
mDensity = dm.density;
final Configuration configuration = resources.getConfiguration();
mScreenWidth = configuration.orientation == Configuration.ORIENTATION_PORTRAIT ? dm.widthPixels : dm.heightPixels;
mScreenHeight = configuration.orientation == Configuration.ORIENTATION_PORTRAIT ? dm.heightPixels : dm.widthPixels;
}
/**
* @param context Application context
*/
public DateSorter(Context context) {
Resources resources = context.getResources();
Calendar c = Calendar.getInstance();
beginningOfDay(c);
// Create the bins
mBins[0] = c.getTimeInMillis(); // Today
c.add(Calendar.DAY_OF_YEAR, -1);
mBins[1] = c.getTimeInMillis(); // Yesterday
c.add(Calendar.DAY_OF_YEAR, -(NUM_DAYS_AGO - 1));
mBins[2] = c.getTimeInMillis(); // Five days ago
c.add(Calendar.DAY_OF_YEAR, NUM_DAYS_AGO); // move back to today
c.add(Calendar.MONTH, -1);
mBins[3] = c.getTimeInMillis(); // One month ago
// build labels
Locale locale = resources.getConfiguration().locale;
if (locale == null) {
locale = Locale.getDefault();
}
LocaleData localeData = LocaleData.get(locale);
mLabels[0] = localeData.today;
mLabels[1] = localeData.yesterday;
int resId = com.android.internal.R.plurals.last_num_days;
String format = resources.getQuantityString(resId, NUM_DAYS_AGO);
mLabels[2] = String.format(format, NUM_DAYS_AGO);
mLabels[3] = context.getString(com.android.internal.R.string.last_month);
mLabels[4] = context.getString(com.android.internal.R.string.older);
}
public static Locale getLocale(Resources res) {
Configuration config = res.getConfiguration();
return Utility.isAtLeastVersion(N) ? config.getLocales().get(0) : config.locale;
}
/**
* コンストラクタ
*
* @param context {@link Context}
*/
FloatingView(final Context context) {
super(context);
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
mMetrics = new DisplayMetrics();
mWindowManager.getDefaultDisplay().getMetrics(mMetrics);
//mParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
mParams.width = (int)(mMetrics.widthPixels * (2/3.f));
mParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
mParams.type = OVERLAY_TYPE;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
mParams.format = PixelFormat.TRANSLUCENT;
// 左下の座標を0とする
mParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
if (Build.VERSION.SDK_INT >= 26) {
mParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
mAnimationHandler = new FloatingAnimationHandler(this);
mLongPressHandler = new LongPressHandler(this);
mMoveEdgeInterpolator = new OvershootInterpolator(MOVE_TO_EDGE_OVERSHOOT_TENSION);
mMoveDirection = FloatingViewManager.MOVE_DIRECTION_DEFAULT;
mUsePhysics = false;
final Resources resources = context.getResources();
mIsTablet = (resources.getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
mRotation = mWindowManager.getDefaultDisplay().getRotation();
mMoveLimitRect = new Rect();
mPositionLimitRect = new Rect();
mSafeInsetRect = new Rect();
// ステータスバーの高さを取得
mBaseStatusBarHeight = getSystemUiDimensionPixelSize(resources, "status_bar_height");
// Check landscape resource id
final int statusBarLandscapeResId = resources.getIdentifier("status_bar_height_landscape", "dimen", "android");
if (statusBarLandscapeResId > 0) {
mBaseStatusBarRotatedHeight = getSystemUiDimensionPixelSize(resources, "status_bar_height_landscape");
} else {
mBaseStatusBarRotatedHeight = mBaseStatusBarHeight;
}
// Init physics-based animation properties
updateViewConfiguration();
// Detect NavigationBar
if (hasSoftNavigationBar()) {
mBaseNavigationBarHeight = getSystemUiDimensionPixelSize(resources, "navigation_bar_height");
final String resName = mIsTablet ? "navigation_bar_height_landscape" : "navigation_bar_width";
mBaseNavigationBarRotatedHeight = getSystemUiDimensionPixelSize(resources, resName);
} else {
mBaseNavigationBarHeight = 0;
mBaseNavigationBarRotatedHeight = 0;
}
mInitialViewWidth = mParams.width;
// Create an instance of OnPinchListner custom class.
OnPinchListener onPinchListener = new OnPinchListener(getContext(), this);
// Create the new scale gesture detector object use above pinch zoom gesture listener.
scaleGestureDetector = new ScaleGestureDetector(getContext(), onPinchListener);
// 初回描画処理用
getViewTreeObserver().addOnPreDrawListener(this);
}
public static boolean isTablet(Resources res){
return (res.getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}