工具栏使用渐变背景设置标题背景透明化

4

我定义了一个Gradient Drawable:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:endColor="#2F3F59"
        android:startColor="#4078A7"
        android:type="linear"
        android:angle="90" />

</shape>

然后我将它设置在我的工具栏上:

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/textcolorsecundary</item>
    <item name="actionMenuTextColor">@color/textcolorsecundary</item>
    <item name="android:textColorSecondary">@color/textcolorsecundary</item>
    <item name="android:background">@drawable/custom_background_blue</item>
</style>

这个是有效的!但是

在此输入图像描述

标题会出现相同的渐变效果,这看起来非常难看,因此我必须更改它。如何将信息文本的背景设置为透明?


请查看此链接--https://dev59.com/wXI_5IYBdhLWcg3wK_3E - Ag.Pro. Dev
你好。我不认为这可以帮助我。我怀疑标题是一个ImageView,而且我也不知道如何访问工具栏标题的背景。 - Mulgard
4个回答

5

您还可以通过将工具栏的背景设置为您的可绘制对象来解决这个问题。

mToolbar().setDrawable(R.drawable.your_gradient)


0

这完全是错的。我不想让我的工具栏透明...我想让标题的背景透明... - Mulgard
我说过,只需创建一个可以在任何地方使用的翻译颜色即可。 - Ag.Pro. Dev
如何访问工具栏标题的背景?这是核心问题。设置透明背景不是问题! - Mulgard

0
解决方案是禁用工具栏的标题:
this.getSupportActionBar().setDisplayShowTitleEnabled(false);

并添加自定义的 TextView

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:elevation="2dp"
    android:focusable="false"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
    app:theme="@style/AppTheme.Toolbar" >

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:background="@null"
        android:text="Toolbar Title"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />

</android.support.v7.widget.Toolbar>

0

在样式部分将背景色改为透明应该可以解决这个问题

<style name="AppTheme.Toolbar" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:background">#00000000</item>
</style>

如何在Android应用程序中使标题背景透明这个问题与上面的问题类似,上面的解决方案起到了作用。该Android应用程序是为Renesas RX130和Texas Instrument CC2650 BLE解决方案设计的。以下是修复后的解决方案。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接