前言

这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用。

声明

  欢迎转载,但请保留文章原始出处:) 
    博客园:http://www.cnblogs.com

    农民伯伯: http://over140.cnblogs.com

正文

1、精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕)

public static double getScreenPhysicalSize(Activity ctx) {
DisplayMetrics dm = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
return diagonalPixels / (160 * dm.density);
}

一般是7寸以上是平板

相关文章:http://www.cnblogs.com/sishuiliuyun/archive/2013/01/08/2851630.html

2、判断是否是平板(官方用法)

public static boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

3、文字根据状态更改颜色 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"/>
</selector>

  放在res/color/目录下

4、背景色根据状态更改颜色 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>

  如果直接给背景色color会报错。

5、启动APK的默认Activity

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、计算字宽

 public static float GetTextWidth(String text, float Size) {
TextPaint FontPaint = new TextPaint();
FontPaint.setTextSize(Size);
return FontPaint.measureText(text);
}

注意如果设置了textStyle,还需要进一步设置TextPaint。

Android 实用代码七段(一)的更多相关文章

  1. Android实用代码七段(五)

      前言  每次分享意味着每次都有进步,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯 ...

  2. Android实用代码七段(四)

    声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.发送不重复的通知(Notif ...

  3. Android 实用代码七段(三)

    前言 终于又攒了一篇出来,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http: ...

  4. Android实用代码七段(一)

    前言 这里积累了一些不常见确又很实用的代码,每收集7条更新一次,希望能对大家有用. 声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: htt ...

  5. Android 实用代码七段(二)

    声明 欢迎转载,但请保留文章原始出处:)  博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.获取应用程序下所有Acti ...

  6. Android实用代码七段(二)

    正文 一.获取应用程序下所有Activity public static ArrayList<String> getActivities(Context ctx) {       Arra ...

  7. Android实用代码七段(三)

    正文  一.获取已经安装APK的路径 PackageManager pm = getPackageManager(); for (ApplicationInfo app : pm.getInstall ...

  8. Android 实用代码片段

    一些不常见确又很实用的代码块. 1.精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) public static double getScreenPhysicalSize(Activity ct ...

  9. Android实用代码模块集锦

    1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx)  ...

随机推荐

  1. crontab环境变量问题

    今天设置linux定时任务时,python内调用的shell指令总执行失败,单独调用python脚本则无问题,考虑到是环境变量未生效引起. 故在执行crontab -e编辑配置文件时,将shell内执 ...

  2. Qt多文档界面应用设计

    使用Qt编写多文档界面(MDI)应用相当方便,主要会使用到QMdiArea和QMdiSubWindow两个类.可以查看Qt Asistant中这两个类的说明文档,里面介绍的相当详细.另外,可以搜索例程 ...

  3. VS2015下的Android开发系列01——开发环境配置及注意事项

    概述 VS自2015把Xamarin集成进去后搞Android开发就爽了,不过这安装VS2015完成的时候却是长了不知道多少.废话少说进正题,VS2015安装时注意把Android相关的组件勾选安装, ...

  4. Ubuntu中PyCharm中字体设置

    在Ubuntu安装的PyCharm与Windows不同,除了Editor中的字体.配色需要设置外,文件操作栏(File--Edit--View--Navigate--Code--Refactor--- ...

  5. Logback 将日志分级别打印

    最近项目中用到了logback 记录日志,  关于为啥使用logback 请百度一下:  logback与Log4J的区别 好了,废话不多说,直奔主题, 研究了好久,终于将日志按级别将日志分文件打印出 ...

  6. UVA 11734 Big Number of Teams will Solve This

    大水题,不解释啦! #include<cstdio> #include<cstring> #define maxn 50 using namespace std; char s ...

  7. 移动开发:初学 iOS-UIViewController 心得

    初学 iOS,本文翻译了一些 iOS 官网上的 UIViewController 的知识点,如有不到位或不正确的地方,还请指正: 本文所介绍的内容的目标: 理解content view control ...

  8. Ajax.BeginForm返回方法OnSuccess

    在MVC3里面——程序集 System.Web.Mvc.dll, v4.0.30319有这么一个Ajax.BeginForm异步登录验证的类型,我们在下面给出一个例子:在登录页面Logion.csht ...

  9. Codeforces Burning Midnight Oil

    /* * BurningMidnightOil.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每次至少写多少行代码ret: * 1 ...

  10. easyui源码翻译1.32--Droppable(放置)

    前言 使用$.fn.droppable.defaults重写默认值对象.下载该插件翻译源码 源码 /** * jQuery EasyUI 1.3.2 * *翻译:lbq --放置 拉伸 */ (fun ...