web-dev-qa-db-ja.com

マテリアルデザインナビゲーションドロワーのハンバーガーアイコンの色を変更する方法

私はこの例をフォローしています

http://www.androidhive.info/2015/04/Android-getting-started-with-material-design/

この例では、ハンバーガーのアイコンを白で表示していますが、カスタマイズして黒にしたいのですが、変更する方法が見つかりません。カスタマイズ方法を教えてください。

マニフェスト

<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
    package="info.androidhive.materialdesign" >


    <uses-permission Android:name="Android.permission.INTERNET"></uses-permission>

    <uses-permission Android:name="Android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <application
        Android:allowBackup="true"
        Android:icon="@mipmap/ic_launcher"
        Android:label="@string/app_name"
        Android:theme="@style/MyMaterialTheme" >
        <activity
            Android:name=".activity.MainActivity"
            Android:label="@string/app_name" >
            <intent-filter>
                <action Android:name="Android.intent.action.MAIN" />

                <category Android:name="Android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

スタイル

<resources>

    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="homeAsUpIndicator">@drawable/hamburger</item>
    </style>

</resources>

主な活動

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {

    private static String TAG = MainActivity.class.getSimpleName();

    private Toolbar mToolbar;
    private FragmentDrawer drawerFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        drawerFragment = (FragmentDrawer)
                getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);

        // display the first navigation drawer view on app launch
        displayView(0);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        if(id == R.id.action_search){
            Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onDrawerItemSelected(View view, int position) {
        displayView(position);
    }

    private void displayView(int position) {
        Fragment fragment = null;
        String title = getString(R.string.app_name);
        switch (position) {
            case 0:
                fragment = new HomeFragment();
                title = getString(R.string.title_home);
                break;
            case 1:
                fragment = new FriendsFragment();
                title = getString(R.string.title_friends);
                break;
            case 2:
                fragment = new MessagesFragment();
                title = getString(R.string.title_messages);
                break;
            case 3:
                fragment = new ContactUsFragment();
                title = getString(R.string.title_contactus);
                break;
            case 4:
                fragment = new AboutUsFragment();
                title = getString(R.string.title_aboutus);
                break;
            default:
                break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.commit();

            // set the toolbar title
            getSupportActionBar().setTitle(title);
        }
    }
72
Aditya

ハンバーガーアイコンの色を変更するには、「style.xml」クラスを開いてから、次のコードを試してください。

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">@Android:color/black</item>
</style>

<item name="color">@Android:color/black</item>行を確認してください。ここで目的の色を変更するだけです。

197
Anand Singh

プログラムでこの行を追加します

actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
48
saigopi

ColorControlNormalのオーバーライドも機能します。

<item name="colorControlNormal">@Android:color/holo_red_dark</item>
12
user1689757

1.Color.xml .<color name="hamburgerBlack">#000000</color>

2. style.xmlで。

<style name="DrawerIcon" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="color">@color/hamburgerBlack</item>
    </style>

3.次に、メインテーマクラス(ファイル名style.xml)。「AppTheme」があります。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
         <item name="drawerArrowStyle">@style/DrawerIcon</item>
    </style>
8
Jigish

yourProject/res/values/styles.xml

style.xmlに以下を追加します。

<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="colorControlNormal">@color/white</item> 
</style>
enter code here :
5
Mr.Hosseini

そのためには、次の手順に従ってください。

protected ActionBarDrawerToggle drawerToggle;

drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));
4
venky
<Android.support.design.widget.AppBarLayout
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:background="@color/colorTransparent"

テーマも削除します

    Android:theme="@style/AppThemeNoActionBar.AppBarOverlay">

    <Android.support.v7.widget.Toolbar
        Android:id="@+id/toolbar"
        Android:layout_width="match_parent"
        Android:layout_height="?attr/actionBarSize"
      ***

これを削除

*** app:popupTheme = "@ style/AppThemeNoActionBar.PopupOverlay">

</Android.support.design.widget.AppBarLayout>

最後にこれを追加

 <style name="AppThemeNoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

あなたのメインヘムに

        <item name="colorControlNormal">@color/colorRedTitle</item>

    </style>
2
Goodlife

App:theme = "@ style/MyMaterialTheme"を設定すると正常に機能します

1
Sudeep Kaushik

ハンバーガーアイコンは、アクションActionBarDrawerToggleクラスによって制御されます。 Android compatライブラリを使用している場合、これは今のところ必須です。次のように色を変更できます。

    toggle?.drawerArrowDrawable?.color = ContextCompat.getColor(context, R.color.colorPrimary)
0
//----------your own toolbar-----------------------------

            <?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:layout_width="match_parent"
                Android:layout_height="wrap_content"
                Android:elevation="4dp"
                Android:padding="1dp"
                Android:background="@color/blue"
                >
                </Android.support.v7.widget.Toolbar>

    //-----------Main activity xml, add your own toolbar-----------------------------------------------
    <RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
        xmlns:tools="http://schemas.Android.com/tools" Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        tools:context="com.v1technologies.sgcarel.FrameActivity">
        <include
            Android:id="@+id/toolbar"
            layout="@layout/toolbar"
            />
        <FrameLayout
            Android:padding="2dp"
            Android:layout_marginTop="70dp"
            Android:id="@+id/frame_frame_activity"
            Android:layout_width="match_parent"
            Android:layout_height="match_parent">

    </FrameLayout>
    </RelativeLayout>


        //----  In your activity-----------------------

                   toolbar = (Toolbar) findViewById(R.id.toolbar);       
                    setSupportActionBar(toolbar);

            //===========================================================================

             @Override
                public boolean onCreateOptionsMenu(Menu menu) {
                    // Inflate the menu; this adds items to the action bar if it is present.
                    getMenuInflater().inflate(R.menu.menu_main, menu);

                    int color = Color.parseColor("#334412");
                    final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);

                    for (int i = 0; i < toolbar.getChildCount(); i++) {
                        final View v = toolbar.getChildAt(i);

                        if (v instanceof ImageButton) {
                            ((ImageButton) v).setColorFilter(colorFilter);
                        }
                    }
                    return true;
                }
0

ナビゲーションドロワーアイコンの色だけを変更する場合は、次を試してください。

<Android.support.design.widget.NavigationView
    app:itemIconTint="@color/thecolorthatyouwant"
 />

activity_drawer.xmlで直接

0
Gabry_Neo