简体   繁体   English

如何更改ActionBar汉堡包图标的颜色?

[英]How do I change the color of the ActionBar hamburger icon?

I'm using NavigationView from android design support library and toolbar and everything works fine, but I want to know how to make the hamburger icon dark (right now it appears white). 我正在使用来自Android设计支持库和工具栏的NavigationView,一切正常,但我想知道如何让汉堡图标变暗(现在它显得很白)。 Also I wanna adjust distance from screen edge to ActionBar Title. 此外,我想调整从屏幕边缘到ActionBar标题的距离。 So how do I do this? 那我该怎么做? Thanks for help. 感谢帮助。

I set theme as Theme.AppCompat.Light.NoActionBar. 我将主题设置为Theme.AppCompat.Light.NoActionBar。 So I use toolbar. 所以我使用工具栏。 Here is my code: 这是我的代码:

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

And xml: 和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"
    />

To change the hamburger icon, just create a new icon (eg ic_my_dark_menu) then assign that to your action bar: 要更改汉堡图标,只需创建一个新图标(例如ic_my_dark_menu),然后将其分配给您的操作栏:

actionBar.setHomeAsUpIndicator(R.drawable.ic_my_dark_menu);

Or, you can tint your existing icon, if you'd rather do it this way: 或者,如果您愿意这样做,您可以为现有图标着色

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

To change amount of space between your action bar title and the edge, just edit your toolbar layout (res/layout/tool_bar.xml). 要更改操作栏标题和边缘之间的空间量,只需编辑工具栏布局(res / layout / tool_bar.xml)。 For example, you can add padding like this: 例如,您可以像这样添加填充:

res/layout/tool_bar.xlml RES /布局/ tool_bar.xlml

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

May be you have to try this ..Apply this theme to toolbar ... 也许你必须尝试这个..将这个主题应用于工具栏......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM