【问题标题】:How do I change the color of the ActionBar hamburger icon?如何更改 ActionBar 汉堡包图标的颜色?
【发布时间】:2015-06-10 15:29:24
【问题描述】:

我正在使用来自 android 设计支持库和工具栏的 NavigationView,一切正常,但我想知道如何使汉堡图标变暗(现在它显示为白色)。另外我想调整从屏幕边缘到 ActionBar Title 的距离。那么我该怎么做呢?感谢您的帮助。

我将主题设置为 Theme.AppCompat.Light.NoActionBar。所以我使用工具栏。这是我的代码:

public class MainActivity extends AppCompatActivity {

//Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Песни","Исполнители"};
int Numboftabs =2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Initializing Toolbar and setting it as the actionbar
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

还有xml:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:context=".MainActivity">

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    >
    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"
        />

    <ru.amdm.amdm.SlidingTabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="2dp"
        android:background="@color/ColorPrimary"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:foreground="?android:windowContentOverlay">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"

            android:layout_height="match_parent"
            android:layout_width="match_parent"/>
    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_gravity="start"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer"
    />

【问题讨论】:

  • 您是仅使用ActionBar 还是ToolBar?请发布您当前的代码。
  • @hungryghost 感谢您的编辑!

标签: android icons navigationview


【解决方案1】:

要更改汉堡图标,只需创建一个新图标(例如 ic_my_dark_menu),然后将其分配给您的操作栏:

actionBar.setHomeAsUpIndicator(R.drawable.ic_my_dark_menu);

或者,您可以tint您现有的图标,如果您愿意这样做:

Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_menu, null);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, Color.BLACK);
actionBar.setHomeAsUpIndicator(drawable);

要更改操作栏标题和边缘之间的空间量,只需编辑工具栏布局 (res/layout/tool_bar.xml)。例如,您可以像这样添加填充:

res/layout/tool_bar.xml

<android.support.v7.widget.Toolbar
    ...
    android:paddingTop="16dp" />

【讨论】:

  • 如何浏览 support.design 库来找到这个汉堡图标的来源?因为我认为它管理图标的外观。或者可能有一些方法或 xml 属性来配置该图标?
  • 正如我在回答中所说,汉堡包图标设置为actionBar.setHomeAsUpIndicator()。使用该方法,您可以指定要用作汉堡包图标的可绘制对象。这就是图标的来源。如果您想进一步配置图标,您可以使用我已经在我的答案中发布的代码进行此操作。
  • 谢谢!它工作正常。我还找到了另一种简单的方法 - 将工具栏的主题设置为 android:theme="@style/ThemeOverlay.AppCompat.Light"
  • 添加 ThemeOverlay 样式会起作用,但它也会影响 ActionBar 的其他方面(如文本颜色、溢出菜单等)。但如果这是你想要的,而不仅仅是汉堡图标,那么改变你的风格是最好的选择。
  • 是的,你是对的。我在这里找到了这个问题的解决方案:stackoverflow.com/a/28205087/4656400
【解决方案2】:

也许你必须试试这个..将此主题应用到工具栏...

<style name="MyThemeOverlay">
   <item name="android:colorControlNormal">@color/myControlColor</item>
</style>

【讨论】:

  • 这只适用于 API 级别 21。我目前的最小值是 15
  • @AlexKost,使用colorControlNormal 而不是android:colorControlNormal。由于 appcompat,这与较低的 API 级别兼容
猜你喜欢
  • 2021-03-17
  • 1970-01-01
  • 1970-01-01
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
相关资源
最近更新 更多