Java 如何更改 Material Design 导航抽屉中汉堡包图标的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31870132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change color of hamburger icon in material design navigation drawer
提问by Aditya
I am following this example
我正在关注这个例子
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
and in this example it is showing hamburger icon white,i want to customize it and make it black,but i am not able to find anything to how to change it,can any one tell how to customize it?
在此示例中,它显示汉堡图标为白色,我想对其进行自定义并将其设为黑色,但是我找不到任何可以更改它的方法,谁能告诉如何自定义它?
Manifest
显现
<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>
style
风格
<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>
MainActivity
主要活动
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);
}
}
采纳答案by Anand Singh
To change color of hamburger icon you have to open "style.xml" class, then try this code:
要更改汉堡包图标的颜色,您必须打开“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>
So check <item name="color">@android:color/black</item>
line. Just change your desired color here.
所以检查<item name="color">@android:color/black</item>
线。只需在此处更改您想要的颜色。
回答by Tarun Voora
After a struggle of 2 hours , this post helped me . In Androidhive material example, change primary color to another for getting new actionbar color. this below code is for getting arrow mark on action bar and making custom text. Finally i understood that, arrow icon will be in appcompat resource files but hamburger icon not present in resources. if it is present we can change it at runtime
经过 2 个小时的斗争,这篇文章帮助了我。在 Androidhive 材质示例中,将原色更改为另一种以获得新的操作栏颜色。下面的代码用于在操作栏上获取箭头标记并制作自定义文本。最后我明白了,箭头图标将在 appcompat 资源文件中,但汉堡图标不存在于资源中。如果存在,我们可以在运行时更改它
setSupportActionBar(toolbar);
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(getResources().getColor(R.color.black), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
getSupportActionBar().setTitle(Html.fromHtml("<font color=\"black\">" + "All Addresses" + "</font>"));
getSupportActionBar().show();
for changing the home button, i followed @anandsingh answer.
为了更改主页按钮,我遵循了@anandsingh 的回答。
回答by Sudeep Kaushik
This works fine when you set app:theme="@style/MyMaterialTheme"
这在您设置 app:theme="@style/MyMaterialTheme" 时工作正常
回答by user1689757
Overriding colorControlNormal also works.
覆盖 colorControlNormal 也有效。
<item name="colorControlNormal">@android:color/holo_red_dark</item>
回答by Raktim Bhattacharya
//----------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;
}
回答by Mr.Hosseini
yourProject/res/values/styles.xml
yourProject/res/values/styles.xml
in the styles.xml add:
在styles.xml中添加:
<style name="BaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/white</item>
</style>
回答by Sai Gopi N
do it programmatically add this line
以编程方式添加这一行
actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.white));
回答by Jigish
1.In Color.xml.<color name="hamburgerBlack">#000000</color>
1.在Color.xml中。<color name="hamburgerBlack">#000000</color>
2.In style.xml.
2.在style.xml中。
<style name="DrawerIcon" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="color">@color/hamburgerBlack</item>
</style>
3. Then your main theme class(File name style.xml).I have “AppTheme”.
3.然后是你的主主题类(文件名style.xml)。我有“AppTheme”。
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="drawerArrowStyle">@style/DrawerIcon</item>
</style>
回答by Gabry_Neo
If you want to change the color just to your navigation drawer icon, try this:
如果您只想将颜色更改为导航抽屉图标,请尝试以下操作:
<android.support.design.widget.NavigationView
app:itemIconTint="@color/thecolorthatyouwant"
/>
straight in your activity_drawer.xml
直接在您的 activity_drawer.xml 中
回答by venky
For that you can proceed as follows:
为此,您可以按以下步骤操作:
protected ActionBarDrawerToggle drawerToggle;
drawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.black));