今天程序折腾了大半天关于andorid手机字体自适应的问题,网上也查了很多资料,但是效果都非常不好。
最后好不容易找到一个适配方法,发个帖子分享下经验吧。
首先我们要给先调用TextView 的setTextSize(int unit, int size) 这个方法.
其中第一个参数可设置如下静态变量:TypedValue.COMPLEX_UNIT_PX : Pixels //像素
TypedValue.COMPLEX_UNIT_SP : Scaled Pixels //sp
TypedValue.COMPLEX_UNIT_DIP : Device Independent Pixels //dip我建议传入像素属性,因为根据效果图来做的话一般很容易能截取到像素的
之后我们在size这边传入
textsize是字体的像素。
public static int getFontSize(Context context, int textSize) {
DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
int screenHeight = dm.heightPixels;
// screenWidth = screenWidth > screenHeight ? screenWidth :
// screenHeight;
int rate = (int) (textSize * (float) screenHeight / 1280);
return rate;
}这样字体自适应问题应该能得以解决的。
Android10.0适配
https://juejin.im/post/5cad5b7ce51d456e5a0728b0
Android 9.0 (API28)
Android 9.0 行为变更
Android P版本应用兼容性适配技术指导
Android P的APP适配总结,让你快人一步
https://www.jianshu.com/p/9e9e902ea039
https://blog.csdn.net/chen_lian_/article/details/81516654
https://mp.weixin.qq.com/s/K9eIN0veW96sjXoczHms5w
https://mp.weixin.qq.com/s/MhWurQy9oOf9OuDsdBLU-w
1、图标适配
https://blog.csdn.net/guolin_blog/article/details/79417483
2、通知适配
https://blog.csdn.net/guolin_blog/article/details/79854070
3、安装APK
https://blog.csdn.net/kac930/article/details/79131671
①通知栏适配
![](http://upload-images.jianshu.io/upload_images/7287583-e1be041dd78ab916.png?imageMogr2/auto-orient/strip|imageView2/2/w/545/format/webp)
②角标功能
值得高兴的是,从8.0系统开始,Google制定了Android系统上的角标规范,也提供了标准的API,长期让开发者头疼的这个问题现在终于可以得到解决了。
那么下面我们就来学习一下如何在Android系统上实现未读角标的效果。
public class MainActivity extends AppCompatActivity{
@TargetApi(Build.VERSION_CODES.O)
private void create NotificationChannel(String channelId, String channelName,intimportance){
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setShowBadge(true);
NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
public void sendSubscribeMsg(View view){
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this, "subscribe")
...
.setNumber(2)
.build();
manager.notify(2, notification);
}
}
可以看到,这里我们主要修改了两个地方。
第一是在创建通知渠道的时候,调用了NotificationChannel的setShowBadge(true)方法,表示允许这个渠道下的通知显示角标。
第二是在创建通知的时候,调用了setNumber()方法,并传入未读消息的数量。
Android7.0
https://blog.csdn.net/qq_17766199/article/details/77404712
1.Android7.0调用相机时出现新的错误:
android.os.FileUriExposedException: file:///storage/emulated/0/photo.jpeg exposed beyond app through ClipData.Item.getUri()
![](http://upload-images.jianshu.io/upload_images/2909848-3481767a822cc1c4.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
在Application的onCreat()方法中添加以下代码:
// android 7.0系统解决拍照的问题
StrictMode.VmPolicy.Builder builder =newStrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
1.应用间共享文件
permissiongen 解决7.0相机拍照崩溃问题
https://github.com/lovedise/PermissionGen
不使用库解决方式:
https://blog.csdn.net/lmj623565791/article/details/72859156
2 APK signature scheme v2
1)只勾选v1签名就是传统方案签署,但是在7.0上不会使用V2安全的验证方式。
2)只勾选V2签名7.0以下会显示未安装,7.0上则会使用了V2安全的验证方式。
3)同时勾选V1和V2则所有版本都没问题。
3、org.apache不支持问题
//在app下build.gradle里面android加上这句话
defaultConfig {
useLibrary 'org.apache.http.legacy'
}
解决权限问题的库:
https://github.com/tbruyelle/RxPermissions(推荐使用)
https://github.com/yanzhenjie/AndPermission
https://github.com/googlesamples/easypermissions
https://github.com/permissions-dispatcher/PermissionsDispatcher
不使用库解决方式:
https://blog.csdn.net/lhp15575865420/article/details/79210696
Android 5.0 (API 21)
1.沉浸式状态栏判断; MaterDegsin风格的实现;
通知栏:使用appcompat-v7库中提供 NotificationCompat.Builder 替换Notification.Builder;
setSmallIcon图片资源只使用alpha图层来进行绘制,而不应该包括RGB图层。换句话说就是图片不能有背景色 ,颜色可以通过setColor设置;
1.Android5.0上 WebView中Http和Https混合问题
/**
* MIXED_CONTENT_ALWAYS_ALLOW:允许从任何来源加载内容,即使起源是不安全的;
* MIXED_CONTENT_NEVER_ALLOW:不允许Https加载Http的内容,即不允许从安全的起源去加载一个不安全的资源;
* MIXED_CONTENT_COMPATIBILITY_MODE:当涉及到混合式内容时,WebView 会尝试去兼容最新Web浏览器的风格。
**/
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
- WebView cookie 存储问题
public static void syncCookie(String url, String key) {
CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
cookieManager.removeSessionCookies(null);
cookieManager.flush();
} else {
cookieManager.removeSessionCookie();
CookieSyncManager.getInstance().sync();
}
cookieManager.setAcceptCookie(true);
cookieManager.setCookie(url, key);
}
- 新增酷炫的转场动画
Android 4.4 (API19)
1.沉浸式状态栏
2.WebView 增加对https请求限制需要去验证https证书,或者 设置忽略所有证书;
2.WebView webview调试功能
WebView.setWebContentsDebuggingEnabled(true);