Ответ 1
WindowInsets описывает набор вложений для содержимого окна.
Другими словами, WindowInsets
имеет один прямоугольник доступной области для приложения (и имеет другую информацию, например isRound
). Доступная область исключает прямоугольник StatusBar
и NavigationBar
.
Если вы просто хотите узнать высоту StatusBar
и NavigationBar
, отметьте this.
Вы можете получить WindowInsets
, как показано ниже.
Следующий пример использует WindowInsetsCompat для совместимости.
В вашем стиле .xml:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
...
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
В вашем AndroidManifest.xml
<application
...
android:theme="@style/AppTheme">
...
</application>
В вашем макете xml: (fitsSystemWindows должен быть установлен для получения WindowInsets.)
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
В вашей деятельности (или в любом месте):
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View container = findViewById(R.id.container);
ViewCompat.setOnApplyWindowInsetsListener(container, new OnApplyWindowInsetsListener() {
@Override
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
//you can do something with insets.
int statusBar = insets.getSystemWindowInsetTop(); //this is height of statusbar
int navigationBar = insets.getStableInsetBottom(); //this is height of navigationbar
Log.d("MainActivity", String.format("%s %s", statusBar, navigationBar));
ViewCompat.onApplyWindowInsets(v, insets);
return insets;
}
});
}
}
WindowInsets выглядит так: