如何改变汉堡图标的颜色材料设计抽屉式导航 [英] How to change color of hamburger icon in material design navigation drawer
问题描述
我下面这个例子
<一个href="http://www.androidhive.info/2015/04/android-getting-started-with-material-design/">http://www.androidhive.info/2015/04/android-getting-started-with-material-design/
在本例中,显示出汉堡图标白,我希望对它进行定制,使之黑,但我无法找到任何东西要怎么去改变它,任何一个可以告诉如何自定义的呢?
清单
&LT;舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=info.androidhive.materialdesign&GT;
&LT;使用-权限的Android:名称=android.permission.INTERNET对&GT;&LT; /使用-许可&GT;
&LT;使用-权限的Android:名称=android.permission.ACCESS_NETWORK_STATE&GT;&LT; /使用-许可&GT;
&lt;应用
机器人:allowBackup =真
机器人:图标=@纹理映射/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ MyMaterialTheme&GT;
&LT;活动
机器人:名称=。activity.MainActivity
机器人:标签=@字符串/ APP_NAME&GT;
&LT;意向滤光器&gt;
&lt;作用机器人:名称=android.intent.action.MAIN/&GT;
&LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
&所述; /意图滤光器&gt;
&LT; /活性GT;
&LT; /用途&gt;
&LT; /舱单&GT;
风格
&LT;资源&GT;
&LT;样式名称=MyMaterialTheme父=MyMaterialTheme.Base&GT;
&LT; /风格&GT;
&LT;样式名称=MyMaterialTheme.Base父=Theme.AppCompat.Light.DarkActionBar&GT;
&LT;项目名称=windowNoTitle&GT;真&LT; /项目&GT;
&LT;项目名称=windowActionBar&GT;假&LT; /项目&GT;
&LT;项目名称=colorPrimary&GT; @色/ colorPrimary&LT; /项目&GT;
&LT;项目名称=colorPrimaryDark&GT; @色/ colorPrimaryDark&LT; /项目&GT;
&LT;项目名称=colorAccent&GT; @色/ colorAccent&LT; /项目&GT;
&LT;项目名称=homeAsUpIndicator&GT; @可绘制/汉堡&LT; /项目&GT;
&LT; /风格&GT;
&LT; /资源&GT;
MainActivity
公共类MainActivity扩展AppCompatActivity实现FragmentDrawer.FragmentDrawerListener {
私有静态字符串变量= MainActivity.class.getSimpleName();
私人工具栏mToolbar;
私人FragmentDrawer drawerFragment;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
mToolbar =(工具栏)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar()setDisplayShowHomeEnabled(真)。
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(本);
//显示在应用程序启动的第一个抽屉式导航视图
displayView(0);
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.menu_main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
// noinspection SimplifiableIfStatement
如果(ID == R.id.action_settings){
返回true;
}
如果(ID == R.id.action_search){
Toast.makeText(getApplicationContext(),搜寻动作被选择!,Toast.LENGTH_SHORT).show();
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@覆盖
公共无效onDrawerItemSelected(查看视图,INT位置){
displayView(位置);
}
私人无效displayView(INT位置){
片段片段= NULL;
字符串标题=的getString(R.string.app_name);
开关(位置){
情况下0:
片段=新HomeFragment();
标题=的getString(R.string.title_home);
打破;
情况1:
片段=新FriendsFragment();
标题=的getString(R.string.title_friends);
打破;
案例2:
片段=新MessagesFragment();
标题=的getString(R.string.title_messages);
打破;
案例3:
片段=新ContactUsFragment();
标题=的getString(R.string.title_contactus);
打破;
壳体4:
片段=新AboutUsFragment();
标题=的getString(R.string.title_aboutus);
打破;
默认:
打破;
}
如果(片段!= NULL){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body,片段);
fragmentTransaction.commit();
//设置工具栏标题
getSupportActionBar()的setTitle(职称)。
}
}
要改变汉堡包图标的颜色你要打开style.xml类,那么试试这个code:
&LT;样式名称=MyMaterialTheme父=MyMaterialTheme.Base&GT;
&LT; /风格&GT;
&LT;样式名称=MyMaterialTheme.Base父=Theme.AppCompat.Light.DarkActionBar&GT;
&LT;项目名称=windowNoTitle&GT;真&LT; /项目&GT;
&LT;项目名称=windowActionBar&GT;假&LT; /项目&GT;
&LT;项目名称=colorPrimary&GT; @色/ colorPrimary&LT; /项目&GT;
&LT;项目名称=colorPrimaryDark&GT; @色/ colorPrimaryDark&LT; /项目&GT;
&LT;项目名称=colorAccent&GT; @色/ colorAccent&LT; /项目&GT;
&LT;项目名称=drawerArrowStyle&GT; @风格/ DrawerArrowStyle&LT; /项目&GT;
&LT; /风格&GT;
&LT;样式名称=DrawerArrowStyle父=@风格/ Widget.AppCompat.DrawerArrowToggle&GT;
&LT;项目名称=spinBars&GT;真&LT; /项目&GT;
&LT;项目名称=色与GT; @android:彩色/黑白LT; /项目&GT;
&LT; /风格&GT;
因此,检查&LT;项目名称=色与GT; @android:彩色/黑白LT; /项目&GT;
行。只是在这里改变你想要的颜色。
I am following this example
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);
}
}
To change color of hamburger icon you have to open "style.xml" class, then try this code:
<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.
这篇关于如何改变汉堡图标的颜色材料设计抽屉式导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!