30095

Toolbar with Gradient Background set title background transparent

<h3>Question</h3>

I defined a 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>

And I set it on my Toolbar:

<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>

This is working! BUT:

This is what happens with the title. It gets the same gradient. This looks really ugly so I have to change this. How can I set the background of that info text to be transparent?


<h3>Answer1:</h3>

You can also solve this by setting the background of the toolbar to your drawable.

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


<h3>Answer2:</h3>

link - Set transparent background of an imageview on Android

use this color in your color xml file and then give this color to your startcolor and end color


<h3>Answer3:</h3>

The solution is to disable the title of the toolbar:

this.getSupportActionBar().setDisplayShowTitleEnabled(false);

and to add a custom 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>
<h3>Answer4:</h3>

Changing the background color to transparent in the style section should fix the issue

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

The question How to make title background transparent on android app had similar issue above solution did the trick. The android app was for Renesas RX130 and Texas Instrument CC2650 BLE solution. Bellow is solution after the fix.

来源:https://stackoverflow.com/questions/28817109/toolbar-with-gradient-background-set-title-background-transparent

Recommend

  • Support for POSIX openat functions in python
  • Installing apk on android device via ADB with Java program on Linux
  • C# Emgu : Use of DISOpticalFlow
  • Unable to find meaningful logs for ReactNative using Crashlytics
  • Core Data Optimize Fetch Request
  • Integrating Angular with ExtJS
  • Trying to initate guided access from within an iOS6 app?
  • “Hello World” with official nginx and php docker images. Howto?
  • how to save settings of eclipse?
  • Android upgrade db not updating db version after successful upgrade
  • Catch SqlException when Attempting NHibernate Transaction
  • How to call a CUDA file from a C++ header file?
  • webScriptNameForSelector and javascript method with underscore
  • How to enrich a TraversableOnce with my own generic map?
  • What is add2 for sortfields
  • Convert Hex to Decimal when no datatype can hold the full number
  • How to install PyGObject through PyCharm
  • Batch script read a file that's continously written
  • FormsAuthentication.CookieDomain property
  • How to fix Invalid JWT with JHipster Registry [Docker]?
  • Waiting for QProcess to finish or duration to exceed [duplicate]
  • Error when build LineageOS: “make: *** [ninja_wrapper] Error 1”
  • UI-Router default view inside div
  • NodeJS - installing local module
  • Wrap array elements in divs based on same value
  • Logout user after Woocommerce Checkout
  • Threads and Concurrent Modification Exception working with a list
  • can you use embedded ruby in custom javascript files in rails?
  • How to debug iBeacons and Passbook
  • How do you run a synchronous timer in C#?
  • Android studio import problems. (Apktool)
  • Use AutoIt with java applications
  • How to select multiple items from a List view - JavaFX 8
  • Comma decimal separator is ignored by ASP.NET MVC model binder