Preface
Here we have accumulated some uncommon and practical code, which is collected every seven updates, hoping to be useful to everyone.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog: http://www.cnblogs.com
Farmer's uncle: http://over140.cnblogs.com
Body
1. Precisely obtain the screen size (for example, 3.5, 4.0, 5.0)
Public static double getScreenPhysicalSize (Activity ctx ){
DisplayMetrics dm = new DisplayMetrics ();
Ctx. getWindowManager (). getdefadisplay display (). getMetrics (dm );
Double diagonalPixels = Math. sqrt (Math. pow (dm. widthPixels, 2) + Math. pow (dm. heightPixels, 2 ));
Return diagonalPixels/(160 * dm. density );
}
Generally, a tablet is larger than 7 inches.
2. determine whether it is a tablet (official usage)
Public static boolean isTablet (Context context ){
Return (context. getResources (). getConfiguration (). screenLayout & Configuration. SCREENLAYOUT_SIZE_MASK)> = Configuration. SCREENLAYOUT_SIZE_LARGE;
}
3. Change the color of text based on the Status android: textColor
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: color = "# 53c1bd" android: state_selected = "true"/>
<Item android: color = "# 53c1bd" android: state_focused = "true"/>
<Item android: color = "# 53c1bd" android: state_pressed = "true"/>
<Item android: color = "#777777" type = "codeph" text = "/codeph"/>
</Selector>
Put it in the res/color/directory
4. the background color is changed based on the status. android: backgroup
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: state_selected = "true"> <shape>
<Gradient android: angle = "0" android: centerColor = "# 00a59f" android: endColor = "# 00a59f" android: startColor = "# 00a59f"/>
</Shape> </item>
<Item android: state_focused = "true"> <shape>
<Gradient android: angle = "0" android: centerColor = "# 00a59f" android: endColor = "# 00a59f" android: startColor = "# 00a59f"/>
</Shape> </item>
<Item android: state_pressed = "true"> <shape>
<Gradient android: angle = "0" android: centerColor = "# 00a59f" android: endColor = "# 00a59f" android: startColor = "# 00a59f"/>
</Shape> </item>
<Item> <shape>
<Gradient android: angle = "0" android: centerColor = "#00ff00" android: endColor = "00ff00" android: startColor = "00ff00"/>
</Shape> </item>
</Selector>
If the background color is directly given, an error is reported.
5. Start the default Activity of the APK
Public static void startApkActivity (final Context ctx, String packageName ){
PackageManager pm = ctx. getPackageManager ();
PackageInfo pi;
Try {
Pi = pm. getPackageInfo (packageName, 0 );
Intent intent = new Intent (Intent. ACTION_MAIN, null );
Intent. addCategory (Intent. CATEGORY_LAUNCHER );
Intent. setPackage (pi. packageName );
List <ResolveInfo> apps = pm. queryIntentActivities (intent, 0 );
ResolveInfo ri = apps. iterator (). next ();
If (ri! = Null ){
String className = ri. activityInfo. name;
Intent. setComponent (new ComponentName (packageName, className ));
Ctx. startActivity (intent );
}
} Catch (NameNotFoundException e ){
Log. e ("startActivity", e );
}
}
7. Calculate the word width
Public static float GetTextWidth (String text, float Size ){
TextPaint FontPaint = new TextPaint ();
FontPaint. setTextSize (Size );
Return FontPaint. measureText (text );
}
NOTE: If textStyle is set, you need to further set TextPaint.