適配全面屏手機的凹凸狀態欄
開屏廣告一般都是全屏的,在非全面屏的手機問世之前 我們只要設置activity的style爲<item name="android:windowFullscreen">true</item>
,即可解決問題,但是全面屏手機出來後,如果僅僅是設置該樣式,我們會發現凹凸部分是全黑並沒有實現全面屏,因此對於全面屏的我們需要特殊的處理
理論基礎:
- Android全面屏手機一般是指寬高比大於1.86
- Android p以及以上谷歌提供了適配凹凸部分的方法 傳送門
但是並不是所有的手機都支持谷歌的全面屏適配方案,例如努比亞 魅族就不支持全面屏的適配方案,這是我們可以採用包含狀態欄的全面屏適配方案,
先看效果圖
非全面屏
全面屏不支持適配全面屏 -努比亞手機
全面屏支持適配全面屏 華爲P20
因爲我們的要求是在虛擬鍵下也要顯示所以纔有這個效果,正常情況下不應該顯示的
首先我們設置activity的樣式
默認values 下的style
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@drawable/splash_bg</item>
</style>
values-v28 下的style
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@drawable/splash_bg</item>
//設置適配全面屏
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
</style>
上面我們說過了有些機型是無法適配全面屏的這時我們要將這些特殊機型給單獨適配
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScreenUtil.transparencyBar(this);
TextView textView = findViewById(R.id.tv_count_down);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) textView.getLayoutParams();
//因爲是全屏的 所以這裏我們需要動態的設置跳過的上邊距
layoutParams.topMargin += ScreenUtil.getStatusBarHeight(this);
textView.setLayoutParams(layoutParams);
//全面屏不支持去除狀態欄 則退出全屏幕 採用更改狀態欄爲透明色的方法
if(isCongainer(android.os.Build.BRAND,getResources().getStringArray(R.array.exit_fullscreen))&&ScreenUtil.getScreentRation(this)>1.86){
exitFullScreen();
}
}
/**
* 退出全面屏手機模式
*/
private void exitFullScreen(){
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
window.setAttributes(attributes);
}
}
private boolean isCongainer(String str,String[] datas){
for (String s:datas){
if (TextUtils.equals(str,s)){
return true;
}
}
return false;
}
}
特殊機型建議存放在String文件中 這樣以後再添加機型就不用修改代碼了
<resources>
<string name="app_name">SplashDemo</string>
<!--廣告全面屏視頻 無法適配頂部凹凸部分的機型-->
<string-array name="exit_fullscreen">
<item>nubia</item>
<item>Meizu</item>
</string-array>
</resources>
xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.AppCompatImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@drawable/ad_bg"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_constraintBottom_toTopOf="@+id/tv_logo"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_logo"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#fff"
android:gravity="center"
android:text="我是APP的LOGO"
android:textColor="#000"
android:textSize="28dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="SpUsage" />
<TextView
android:id="@+id/tv_count_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳過 3"
android:textSize="12dp"
android:layout_marginTop="10dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="15dp"
android:textColor="#000"
/>
</android.support.constraint.ConstraintLayout>