下面列出了android.content.res.Configuration#SCREENLAYOUT_SIZE_SMALL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private ScreenType getScreenType(Context context) {
int type = context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
ScreenType result;
switch (type) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
result = ScreenType.SMALL;
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
result = ScreenType.NORMAL;
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
result = ScreenType.LARGE;
break;
default:
result = ScreenType.NORMAL;
break;
}
return result;
}
/**
* detect and initialize text size
*/
public static void textSizeDetection(SharedPreferences sharedPreferences) {
userTextSize = sharedPreferences.getInt(SHP_SETTING.KEY_MESSAGE_TEXT_SIZE, 14);
if (!G.context.getResources().getBoolean(R.bool.isTablet)) {
int screenLayout = context.getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
userTextSize = (userTextSize * 3) / 4;
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
userTextSize = (userTextSize * 3) / 2;
break;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:// or 4
userTextSize *= 2;
}
}
}
/**
* Gets current screen size.
*
* @param context
* the context.
* @return current screen size.
*/
public static ScreenSize getCurrent(Context context) {
switch (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
return SMALL;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return NORMAL;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
return LARGE;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
return XLARGE;
default:
return UNDEFINED;
}
}
/**
* Gets current screen size.
*
* @param context
* the context.
* @return current screen size.
*/
public static ScreenSize getCurrent(Context context) {
switch (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
return SMALL;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return NORMAL;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
return LARGE;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
return XLARGE;
default:
return UNDEFINED;
}
}
private void printDebugInfo() {
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
DebugLog.i("Device density: " + displaymetrics.densityDpi);
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
DebugLog.i("Device size is: LARGE");
}
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
DebugLog.i("Device size is: XLARGE");
}
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
DebugLog.i("Device size is: NORMAL");
}
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
DebugLog.i("Device size is: SMALL");
}
DebugLog.i("Device is tablet: " + UIUtils.isHoneycombTablet(this));
}
public static String getScreenSizeAsString(Context context) {
String size = null;
int screenLayout = context.getResources().getConfiguration().screenLayout;
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
size = "large";
}
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
size = "xlarge";
}
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
size = "normal";
}
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
size = "small";
}
if ((screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_UNDEFINED) {
size = "undefined";
}
return size;
}
/**
* Get the column count according to the current configuration.
* <p>It will also consider multi-window mode on API 24 and above devices.
*
* <p>It is not recommended to do this calculation at runtime. So, please define all the
* span counts in xml.
*
* @param context The context to get configuration.
* @param defaultCount The default column count.
* @param maxCount The maximum column count up to which we can scale.
* @param compact {@code true} if the layout is compact and disable the auto increase of
* columns in multi-window mode.
*
* @return The column count according to the current device configurations.
*/
@TargetApi(Build.VERSION_CODES.N)
public static int getLayoutColumns(@NonNull Context context,
int defaultCount, int maxCount, boolean compact) {
int columns = defaultCount;
int screenCategory = getScreenSizeCategory(context);
switch (screenCategory) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
if (columns > 1) {
columns = columns - 1;
}
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
if (context.getResources().getConfiguration().orientation
!= Configuration.ORIENTATION_LANDSCAPE) {
break;
}
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
columns += 1;
break;
}
if (context.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE){
try {
if (compact || !isInMultiWindowMode(context)) {
columns *= 2;
}
} catch (Exception ignored) {
}
}
return Math.min(columns, maxCount);
}
private String checkScreenSize() {
String screenSize;
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
screenSize = "xlarge";
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
screenSize = "large";
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
screenSize = "normal";
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
screenSize = "small";
} else {
screenSize = "normal";
}
return screenSize;
}
private String checkScreenSize() {
String screenSize = "normal";
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
screenSize = "xlarge";
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
screenSize = "large";
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
screenSize = "normal";
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
screenSize = "small";
} else {
screenSize = "normal";
}
return screenSize;
}
private String checkScreenSize() {
String screenSize = "normal";
if ((cordova.getActivity().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
screenSize = "xlarge";
} else if ((cordova.getActivity().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
screenSize = "large";
} else if ((cordova.getActivity().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
screenSize = "normal";
} else if ((cordova.getActivity().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
screenSize = "small";
} else {
screenSize = "normal";
}
return screenSize;
}
/**
* @param context Context instance
* @return [true] if the device has a small screen, [false] otherwise.
*/
public static boolean hasSmallScreen(Context context) {
if (context == null) return false;
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_SMALL;
}
protected TextView createDefaultTabView(Context context, int i) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP * 2);
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) (TAB_VIEW_TEXT_SIZE_SP * 1.5));
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, (float) (TAB_VIEW_TEXT_SIZE_SP * .75));
} else {
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
}
// textView.setTypeface(custom_font);
textView.setTextColor(Color.WHITE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) {// allcaps is for api 14 and upper
textView.setAllCaps(false);
}
if (i == 0) {
textView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 8.5f));
} else if (i == 1) {
textView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 8.5f));
} else if (i == 2) {
textView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 8.5f));
} else {
textView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 8.5f));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
}
float dp = getResources().getDisplayMetrics().density;
textView.setPadding(2, (int) (20 * dp), 2, (int) (20 * dp));
return textView;
}
public static boolean isSmallScreen(Context context) {
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_SMALL;
}
/**
* @param context Context instance
* @return [true] if the device has a small screen, [false] otherwise.
*/
public static boolean hasSmallScreen(Context context) {
return getScreenSize(context) == Configuration.SCREENLAYOUT_SIZE_SMALL;
}