這裏主要是想和你們討論下頭條適配原文使用中出現的一些問題和解決方法 有更好的方法或者有什麼不對的歡迎批評php
啓動頁圖片問題java
activity
指定一個背景,不設置佈局讓程序快速進入啓動頁<!--啓動頁主題-->
<style name="ThemeSplash" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:background">@drawable/layer_bg_splash</item> <item name="android:windowFullscreen">true</item> <item name="android:windowTranslucentNavigation">true</item> </style>
<!--啓動頁 背景-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/bg_main_color" />
<item android:gravity="center">
<bitmap android:gravity="center" android:src="@drawable/ic_logo_white" />
</item>
</layer-list>
複製代碼
activity
應用還在初始化,onCreate
尚未執行,默認顯示了背景圖,此時的背景圖上的logo
是沒有使用適配方案的效果,當onCrate
被執行後,屏幕的密度發生變化,logo
會放大縮小。<item name="android:windowDisablePreview">true</item>
,讓app
加載完後進入啓動頁橫豎切換適配無效android
android:configChanges=
,在咱們切換了屏幕方向,或者其它操做時會調用:@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Logger.e("當前屏幕爲橫屏");
} else {
Logger.e("當前屏幕爲豎屏");
//退出全屏後從新適配屏幕
ScreenFitUtils.setCustomDensity(this, getApplication());
}
}
複製代碼
咱們在看下super
調用的父類方法作了什麼app
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
if (mResources != null) {
// The real (and thus managed) resources object was already updated
// by ResourcesManager, so pull the current metrics from there.
final DisplayMetrics newMetrics = super.getResources().getDisplayMetrics();
mResources.updateConfiguration(newConfig, newMetrics);
}
}
複製代碼
能夠看到被重置了,因此咱們在前面的適配又變成了原始的數據,若是這時候你刷新界面,界面會變成沒有適配的效果。ide
第三方庫問題佈局
if (sRoncompatDennsity == 0) {
sRoncompatDennsity = appDisplayMetrics.density;
sRoncompatScaledDensity = appDisplayMetrics.scaledDensity;
application.registerComponentCallbacks(new ComponentCallbacks() {
@Override
public void onConfigurationChanged(Configuration newConfig) {
if (newConfig != null && newConfig.fontScale > 0) {
sRoncompatScaledDensity = application.getResources().getDisplayMetrics().scaledDensity;
}
}
@Override
public void onLowMemory() {}
});
}
複製代碼
當節操播放器橫屏播放退出時系統調用了onConfigurationChanged
(我也不知道爲啥)方法,並且scaledDensity
發生了變化(個人設備是pixel默認2.6,適配後3.0,退出全屏變成了4.0)。最糟糕的是隻要sRoncompatDennsity
不等於0就不會去獲取系統默認的density,scaledDensity
致使字體變得超級大字體
registerComponentCallbacks
監聽這樣系統修改文字不能實時更新if (sRoncompatDennsity == 0)
每次都去獲取系統的密度參數AlertDialog
等彈窗超出屏幕this
由於dialog用的是activity
的context
,因此被動的被適配了,密度發生了變化。spa
<style name="CustomAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert"> <!--<item name="android:windowBackground">@drawable/shape_dialog_bg</item>--> <!-- 寬度 --> <item name="android:windowMinWidthMinor">300dp</item> <item name="windowFixedWidthMinor">300dp</item> </style>
複製代碼
Window window = dialog.getWindow();
WindowManager.LayoutParams layoutParams;
if (window != null) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
window.getDecorView().setPadding(0, 0, 0, 0);
layoutParams = window.getAttributes();
layoutParams.width = RuleUtils.getScreenWidth(context);
window.setAttributes(layoutParams);
window.setGravity(Gravity.CENTER);
window.setWindowAnimations(R.style.PopScaleAnimStyle);
}
複製代碼