我有一个 *** 作栏,但一直只是在其中显示应用程序的标题或/和当前片段活动的标题.我想在其中显示徽标,并且添加徽标的任何尝试均未成功.
下面是代码:
public class MainActivity extends ActionBaractivity { private String[] mOptionMenu; private DrawerLayout mDrawerLayout; private relativeLayout mDrawerrelativeLayout; private ListVIEw mDrawerList; private ActionbarDrawerToggle mDrawerToggle; private CharSequence mTitleSection; private CharSequence mTitleApp; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ImageVIEw button=(ImageVIEw)findVIEwByID(R.ID.logobutton); mOptionMenu = new String[] { "Opción 1", "Opción 2", "Opción 3" }; mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerrelativeLayout = (relativeLayout) findVIEwByID(R.ID.left_drawer); mDrawerList = (ListVIEw) findVIEwByID(R.ID.List_vIEw_drawer); mDrawerList.setAdapter(new ArrayAdapter<String>(this, androID.R.layout.simple_List_item_1, mOptionMenu)); mDrawerList.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID) { Fragment fragment = null; switch (position) { case 0: fragment = new FirstFragment(); break; case 1: fragment = new SecondFragment(); break; case 2: fragment = new ThirdFragment(); break; } FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.ID.content_frame, fragment).commit(); mDrawerList.setItemChecked(position, true); mTitleSection = mOptionMenu[position]; getSupportActionbar().setTitle(mTitleSection); mDrawerLayout.closeDrawer(mDrawerrelativeLayout); } }); mDrawerList.setItemChecked(0, true); mTitleSection = getTitle(); mTitleApp = getTitle(); mDrawerToggle = new ActionbarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) { public voID onDrawerClosed(VIEw vIEw) { getSupportActionbar().setTitle(mTitleSection); getSupportActionbar().setlogo(R.drawable.logo); ActivityCompat.invalIDateOptionsMenu(MainActivity.this); } public voID onDrawerOpened(VIEw drawerVIEw) { getSupportActionbar().setTitle(mTitleSection); ActivityCompat.invalIDateOptionsMenu(MainActivity.this); getSupportActionbar().setlogo(R.drawable.logo); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); getSupportActionbar().setdisplayHomeAsUpEnabled(true); getSupportActionbar().setHomebuttonEnabled(true); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_activity_actions, menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { if (mDrawerToggle.onoptionsItemSelected(item)) { return true; } switch (item.getItemID()) { case R.ID.action_settings: Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show(); ; break; default: return super.onoptionsItemSelected(item); } return true; } @OverrIDe protected voID onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @OverrIDe public voID onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); }}
我尝试在代码中添加此行,但未显示任何图像:
getSupportActionbar().setlogo(R.drawable.logo);
我也有类似的担忧,因为我希望徽标是可单击的并将用户定向到MainActivity
解决方法:
您必须通过调用getSupportActionbar().setdisplayUselogoEnabled(true)在Actionbar上启用徽标功能.
总结以上是内存溢出为你收集整理的java-在ActionBar中显示徽标全部内容,希望文章能够帮你解决java-在ActionBar中显示徽标所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)