自Android P發(fā)布以來(lái),陸陸續(xù)續(xù)的有用戶向我反映Android P下輸入法存在導(dǎo)航欄變黑的問(wèn)題,情況如下所示。
于是我抽時(shí)間研究了一下這個(gè)問(wèn)題。
經(jīng)過(guò)一番搜索,我在Simple Keyboard下找到了解決方案,其代碼大致如下:
private int mOriginalNavBarColor = 0;
private int mOriginalNavBarFlags = 0;
......
private void setNavigationBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
......
final Window window = getWindow().getWindow();
if (window == null) {
return;
}
mOriginalNavBarColor = window.getNavigationBarColor();
window.setNavigationBarColor(keyboardColor);
final View view = window.getDecorView();
mOriginalNavBarFlags = view.getSystemUiVisibility();
if (ResourceUtils.isBrightColor(keyboardColor)) {
view.setSystemUiVisibility(mOriginalNavBarFlags | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
} else {
view.setSystemUiVisibility(mOriginalNavBarFlags & ~View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
}
}
}
private void clearNavigationBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
final Window window = getWindow().getWindow();
if (window == null) {
return;
}
window.setNavigationBarColor(mOriginalNavBarColor);
final View view = window.getDecorView();
view.setSystemUiVisibility(mOriginalNavBarFlags);
}
}
具體的原理,我也不班門弄斧了,想要深入了解的同學(xué)請(qǐng)自行百度(代碼中的ResourceUtils.isBrightColor實(shí)現(xiàn),我在后面的C#代碼中給出)。而因?yàn)槲沂褂肵amarin開(kāi)發(fā)的歲寒輸入法,所以不能簡(jiǎn)單地Ctrl+V、Ctrl+C,需要將其轉(zhuǎn)寫(xiě)成C#代碼,說(shuō)白了,就是抄。但這一抄,抄出了大問(wèn)題。
Xamarin.Android會(huì)對(duì)Android的原生API進(jìn)行二次封裝,一般而言,get/set方法會(huì)轉(zhuǎn)換成屬性,flag值會(huì)封裝成枚舉型。
在這個(gè)場(chǎng)景下,具體就是:
View.getSystemUiVisibility()/View.setSystemUiVisibility(int) =>StatusBarVisibility View.SystemUiVisibility {get;set;}
其中StatusBarVisibility就是setSystemUiVisibility所能接受的flag值的封裝。然而這里出現(xiàn)了錯(cuò)誤!
setSystemUiVisibility方法所能接受flag值如下圖:
而StatusBarVisibility的內(nèi)容是這樣的:
顯然,這兩者是對(duì)不上的。
經(jīng)過(guò)研究,我發(fā)現(xiàn)有另一個(gè)枚舉類型——SystemUiFlags,他的內(nèi)容是這樣的:
顯而易見(jiàn),SystemUiFlags正是對(duì)setSystemUiVisibility方法所能接受的flag值的正確封裝。可是為什么 View.SystemUiVisibility的類型會(huì)是StatusBarVisibility呢?
我在Xamarin.Android的GitHub項(xiàng)目的下發(fā)現(xiàn)了一些相關(guān)的issue,才確認(rèn),這確實(shí)是Xamarin.Android的的封裝錯(cuò)誤,而且年代久遠(yuǎn),因?yàn)闊o(wú)法修復(fù)。這里有bugzilla上的bug反饋記錄。
從bug記錄中的官方回復(fù)可知,如果對(duì)這個(gè)問(wèn)題進(jìn)行更正,會(huì)破壞Xamarin.Android的二進(jìn)制接口(ABI)。
官方如此解釋這種破壞的原因:
所以這個(gè)封裝錯(cuò)誤將不會(huì)得到修正,只能將錯(cuò)就錯(cuò)。
如此一來(lái),難道這個(gè)接口就不能用了嗎?倒也不是。
官方有云:
The fact that the type is an enum is *irrelevant*. The fact that values of the type can be cast to other types is irrelevant. What matters are the actual types, as contained in the assembly: System.StringComparison != System.TypeCode. (Note in particular that both StringComparison and TypeCode are enumeration types with
intas the underlying enum type.)
簡(jiǎn)單而言就是,枚舉型本質(zhì)上還是int型,對(duì)其進(jìn)行強(qiáng)制類型轉(zhuǎn)換不會(huì)改變其本質(zhì)。所以,解決方案就是在適當(dāng)?shù)臅r(shí)候使用強(qiáng)制類型轉(zhuǎn)換來(lái)解決問(wèn)題,雖然代碼會(huì)因此看上去有點(diǎn)丑。
int originalNavBarColor;
int originalNavBarFlags;
private void setNavigationBarColor() {
if (Build.VERSION.SdkInt >= BuildVersionCodes.P) {
Window window = this.Window.Window;
if (window == null) return;
originalNavBarColor = window.NavigationBarColor;
window.SetNavigationBarColor(newColor);
var view = window.DecorView;
originalNavBarFlags = (int)view.SystemUiVisibility;
if (isBrightColor(color)) {
view.SystemUiVisibility = (StatusBarVisibility)(originalNavBarFlags | (int)SystemUiFlags.LightNavigationBar);
} else {
view.SystemUiVisibility = (StatusBarVisibility)(originalNavBarFlags & ~(int)SystemUiFlags.LightNavigationBar);
}
}
}
private void clearNavigationBarColor() {
if (Build.VERSION.SdkInt >= BuildVersionCodes.P) {
Window window = this.Window.Window;
if (window == null) return;
window.SetNavigationBarColor(new Color(originalNavBarColor));
window.DecorView.SystemUiVisibility = (StatusBarVisibility)originalNavBarFlags;
}
}
static bool isBrightColor(int color) {
if (Android.Resource.Color.Transparent == color) {
return true;
}
// See http://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
bool bright = false;
int[] rgb = { Color.GetRedComponent(color), Color.GetGreenComponent(color), Color.GetBlueComponent(color) };
int brightness = (int)Math.Sqrt(rgb[0] * rgb[0] * .241 + rgb[1] * rgb[1] * .691 + rgb[2] * rgb[2] * .068);
if (brightness >= 210) {
bright = true;
}
return bright;
}
以上。
希望本文能對(duì)你有所幫助,感謝閱讀。