Используя RelativeLayout
вместо CoordinatorLayout
в качестве макета root, я могу избежать выталкивания содержимого панелью инструментов, но она немного подталкивает, когда я скрываю строку состояния.
Теперь ваша проблема заключается в том, что строка состояния выталкивает содержимое активности, когда она отображается.
Итак, чтобы решить эту проблему, вы можете сделать так, чтобы строка состояния перекрывалась с окном активности, добавив ниже к методу действия onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
ОБНОВЛЕНИЕ
что использовать, если SDK сборки ниже, чем kitkat?
Для поддержки API ниже Kitkat попробуйте создать файл style. xml для квалификатора (v-19) и добавьте следующие атрибуты:
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
UPDATE: используя настраиваемый ActionBar
Layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:text="@string/long_text"
android:textColor="@android:color/white" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#9E0557B5"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Поведение
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
setSupportActionBar(toolbar);
TextView content = findViewById(R.id.content);
content.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (getSupportActionBar().isShowing()) {
getSupportActionBar().hide();
showSystemBar(false);
}
else {
getSupportActionBar().show();
showSystemBar(true);
}
}
});
}
private void showSystemBar(boolean isDisplayed) {
int uiOptions;
if (isDisplayed) {
// show status bar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
getWindow().getDecorView().setSystemUiVisibility(uiOptions);
}
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
// hide status bar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
getWindow().getDecorView().setSystemUiVisibility(uiOptions);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
}
Использовать NoActionBar
стиль int темы. xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
Переопределить стиль. xml (v.19 )
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
Предварительный просмотр