Android实用代码七段(一)

来源:互联网 发布:prototype.js 1.6手册 编辑:程序博客网 时间:2024/05/20 14:18

正文 

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寸以上是平板

 

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。

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 猫尿血怎么办吃什么药 肝癌小便不出来怎么办 怀孕三个月结石血尿怎么办? 肾小球滤过率65怎么办 肾穿后有血肿6cm怎么办 肾穿刺后血肿怎么办 手术后有血肿怎么办 尿蛋白胆红素高怎么办 哺乳期尿蛋白高怎么办 产后老放屁便秘怎么办 肝癌移植后复发怎么办 产后屁多便秘怎么办 肝癌切除后复发怎么办 宝宝便秘拉不出大便怎么办 宝宝便秘屁多怎么办 尿液浑浊气味重怎么办 经常便秘肚子胀怎么办 顺产满月后腰疼怎么办 化验尿蛋白质高怎么办 小孩子反复蛋白尿怎么办呢? 肾上张了个囊肿怎么办 同床后出血怎么办 药物引起胃不适怎么办 感冒引起的便秘怎么办 月经期失血过多怎么办 肾结石尿血好痛怎么办 小孩高烧39.6度怎么办 小孩39℃高烧怎么办 孩孑积食发烧怎么办 尿频尿痛尿血怎么办 膀胱癌血尿堵塞拉不出尿怎么办 憋尿引起尿频怎么办 莫名恶心想吐怎么办 肾结石引起的肾积水怎么办 胃胀肚子痛怎么办啊 肚子痛和胃胀怎么办 肚脐下肚子胀痛怎么办 肾结石患者出现血尿怎么办 孕中肾结石尿血怎么办 小肚子痛尿血尿怎么办 搬重物后手发抖怎么办