【问题标题】:Toolbar with Gradient Background set title background transparent带有渐变背景的工具栏设置标题背景透明
【发布时间】:2015-05-03 05:41:04
【问题描述】:

我定义了一个 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>

这行得通! 但是:

这就是标题所发生的事情。它得到相同的渐变。这看起来真的很难看,所以我必须改变它。如何将该信息文本的背景设置为透明?

【问题讨论】:

标签: android


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    链接 - Set transparent background of an imageview on Android

    在您的颜色 xml 文件中使用此颜色,然后将此颜色赋予您的开始颜色和结束颜色

    【讨论】:

    • 这是完全错误的。我不希望我的工具栏是透明的......我希望标题的背景是透明的......
    • 我说只创建可以在任何地方使用的翻译颜色
    • 以及如何访问工具栏标题的背景?这就是核心问题。设置透明背景不是问题!
    【解决方案3】:

    解决办法是禁用工具栏的标题:

    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>
    

    【讨论】:

      【解决方案4】:

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

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

      How to make title background transparent on android app 的问题与上述解决方案有类似的问题。 android 应用程序适用于 Renesas RX130 和 Texas Instrument CC2650 BLE 解决方案。贝娄是修复后的解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        • 2012-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-26
        相关资源
        最近更新 更多