看了无数关于如何应用gradient drawable
作为Toolbar
背景的帖子后,我发现了下面的代码。但是,这会导致两个问题:1)整个活动的背景具有与背景相同的可绘制渐变; 2)如您在布局预览中看到的Toolbar
和status bar
重叠。
我猜测可以通过添加另一个布局(例如另一个ConstraintLayout
)并将我的偏好颜色用作背景来解决第一个问题,但这会有效吗?
关于第二期,我几乎一无所知。我从未使用过状态栏。这是我正在使用的标志之一吗?我应该以某种方式将整个工具栏限制在状态栏下方吗在它的dp
中添加一些top constraint
?
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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_height="match_parent"
android:layout_width="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/toolbar_gradient"
android:elevation="4dp"
android:minHeight="70dp"
android:theme="@style/AppTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="19dp">
<ImageView
android:id="@+id/weatherImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:contentDescription="weather"
android:src="@mipmap/weather_placeholder_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/weatherTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginStart="8dp"
android:fontFamily="@font/montserrat_bold"
android:gravity="start"
android:text="@string/home_weather_text_placeholder"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/weatherImg"
app:layout_constraintStart_toEndOf="@+id/weatherImg"
app:layout_constraintTop_toTopOf="@+id/weatherImg" />
<TextView
android:id="@+id/title_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:fontFamily="@font/montserrat_bold"
android:gravity="center"
android:text="@string/home"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/weatherImg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/weatherImg" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
<!-- Using a view with a gradient to create a drop navbar_shadow effect for the navbar -->
<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:layout_above="@id/bottom_nav_bar"
android:background="@drawable/navbar_shadow"
app:layout_constraintBottom_toTopOf="@+id/bottom_nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@android:color/white"
app:itemIconTint="@drawable/nav_selected_item_color"
app:itemTextColor="@drawable/nav_selected_item_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_nav_bar">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java,我在setStatusBarGradient()
方法之前调用setContentView()
:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setStatusBarGradient(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = activity.getWindow();
Drawable background = activity.getResources().getDrawable(R.drawable.toolbar_gradient);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); // causing the issue
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.setStatusBarColor(activity.getResources().getColor(android.R.color.transparent));
window.setNavigationBarColor(activity.getResources().getColor(android.R.color.transparent));
window.setBackgroundDrawable(background);
}
}
而我的AppTheme
风格有这两行:
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
我尝试过使用android:fitsSystemWindows="true"
,但那里没有运气。
当前重叠的问题: