How to remove text and logo OR add Overflow on And

2019-06-10 18:35发布

问题:

Have researched this to death and just cannot find the answer.

I have an ActionBar using AppCompat. I am supporting up to API v7 (API v8 would do). ActionBar works perfectly from API v11. API < v11 has the problem of removing an icon (and thus a feature) from the ActionBar without supplying an Overflow. The big picture is that I have an App Logo, an App Title, and 3 Icons all squidging into the same ActionBar on a low end phone. Trying to make some room!

I would be happy with 1 of 2 solutions. Either:

  1. A method of getting an Overflow so that the functionality can be retrieved from a pull down.
  2. (Preferred) A method of removing the icon and text from ActionBar

Below is my current XML for API 11+:

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/LiteActionBarStyle</item>
        <item name="actionBarStyle">@style/LiteActionBarStyle</item>
    </style>

    <style name="LiteActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:displayOptions"></item>
    </style>

</resources>

Specifically:

<item name="android:displayOptions"></item>

This iss the attribute that is unavailable before API v11.

This is where I am so far:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/LiteActionBarStyle</item>
    </style>

    <style name="LiteActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"></style>

</resources>

What is the alternative for APIs prior to v11?

EDIT:

Updated Style with transparency suggestion:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarStyle">@style/LiteActionBarStyle</item>
    </style>

    <style name="LiteActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:icon">@android:color/transparent</item>
    </style>

</resources>

回答1:

Solved it!

When working with API < v11, you have to remove the "android:" element of the attribute in Style. For example:

<style name="LiteActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="displayOptions"></item>
    </style>

This is how it should look in Styles with API >= v11

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/LiteActionBarStyle</item>
        <item name="actionBarStyle">@style/LiteActionBarStyle</item>
    </style>

    <style name="LiteActionBarStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:displayOptions"></item>
    </style>

Also, I was missing some Icons when I applied this on device with API < v11. I had to change the Icons to Always display:

<item
        android:id="@+id/actionInvertShake"
        MyApp:showAsAction="always"
        android:contentDescription="@string/shakeChangeContentDescription"
        android:icon="@drawable/shake"
        android:title="@string/shakeOption"/>

Note that the Namespace is "MyApp" rather than "android"

This removes the Title and Icon of App from ActionBar and displays all icons from Right to Left.



回答2:

Set <item name="android:icon">@android:color/transparent</item> It will not take up any space in the ActionBar.