android - 在工具栏上应用渐变背景
问题描述
我创建了一个自定义渐变可绘制文件,我想将其用作我Toolbar
的背景,但我似乎无法应用它。它只是显示为纯灰色(见图)。难道我做错了什么?我查看了多个帖子,但到目前为止没有什么对我有用。
活动主.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<Toolbar
android:id="@+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/home_toolbar_gradient"
android:elevation="4dp"
android:minHeight="70dp"
android:theme="@style/customToolbar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/title_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/Montserrat-Bold"
android:gravity="center"
android:text="@string/home"
android:textAlignment="center"
android:textColor="@android:color/white" />
</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" />
<com.google.android.material.bottomnavigation.BottomNavigationView
app:labelVisibilityMode="labeled"
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:menu="@menu/bottom_nav_bar">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
home_toolbar_gradient.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="@color/toolbar_starting_color"
android:endColor="@color/toolbar_ending_color"
android:angle="75" />
</shape>
样式.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:fontFamily">@font/montserrat_medium</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- Custom Rounded Dialog style -->
<style name="RoundedDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/rounded_corners</item>
</style>
<style name="customToolbar" parent="Theme.AppCompat.Light.NoActionBar" />
</resources>
在 MainActivity.java 中:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar homeToolbar = findViewById(R.id.home_toolbar);
setSupportActionBar(homeToolbar);
在清单中:
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
解决方案
尝试动态进行:
setSupportActionBar(homeToolbar);
ActionBar actionbar = getSupportActionBar();
actionbar.setBackgroundDrawable(getResources().getDrawable(R.drawable.home_toolbar_gradient));
读这个。它说 - 渐变的角度,仅用于线性渐变。必须是 [0, 315] 范围内的 45 的倍数。
因此,将 home_toolbar_gradient.xml 可绘制更改为
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:type="linear"
android:startColor="#FF33"
android:endColor="#AFF"
android:angle="45" />
</shape>
推荐阅读
- azure-devops - 如何在 YAML 中使用 VSTS 管道变量?
- css - 我的 CSS 形状的圆角问题
- visual-studio-code - vscode:如何在片段*未*选择后制作文本?
- python - 用相应bin的平均值替换列中的数据?
- wordpress - 如何从wordpress中的类别url中删除类别词
- winforms - 如果用户通过关闭终端(Winforms)关闭应用程序,如何捕获关闭事件?
- python - 在 Python 中使用 api 请求时迭代 xml 正文中的页面
- reactjs - 盖茨比仅在刷新时出现
- java - 如何让 Springboot 的 ShutdownHook 等待,直到它的所有处理请求都完成?
- ag-grid - AgGridReact 的 startEditingCell 无法与自定义单元格编辑器组件一起使用