gpt4 book ai didi

android - 抽屉导航图标不会替换插入符号

转载 作者:行者123 更新时间:2023-11-29 15:11:15 24 4
gpt4 key购买 nike

我正在尝试实现一个带有自定义抽屉图标的简单抽屉导航。

  • 我想要的 - 使用自定义抽屉图标而不是向上插入符号。
  • 有什么问题- 自定义图标未显示。这是始终可见的“向上”插入符号。
  • 我做了什么-

  • 使用 Android studio 创建了一个 NavDrawer 项目。

  • 在 Stackoverflow 上搜索了类似的问题。我有 onPostCreate() 方法的引用,但我已经处理好了。

  • 使用 Android 示例应用来运行项目。

  • 查看图片- 查看左上角。向上插入符是我想用我的图标替换的内容

主要 Activity

包 com.deep.wealthtrackernew;

    import android.content.res.Configuration;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import java.util.List;


public class MainActivity extends ActionBarActivity {

private ListView listView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.left_drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer,
R.string.open_drawer, R.string.close_drawer) {

@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// getActionBar().setTitle();
supportInvalidateOptionsMenu();
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle("Wealth Tracker");
supportInvalidateOptionsMenu();
}
};

drawerLayout.setDrawerListener(actionBarDrawerToggle);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}

@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.

if(actionBarDrawerToggle.onOptionsItemSelected(item)){ return true; }

int id = item.getItemId();

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

return super.onOptionsItemSelected(item);
}
}

See the top left corner. The up caret is what I would like to replace with my icon

最佳答案

在您的主要 Activity oncreate 中,在 setContentView 之后立即执行此操作:

  //if you use support action bar it is
ActionBar actionBar=getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.newIcon);
//if you are not using support action bar it is
ActionBar actionBar=getActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.newIcon);

关于android - 抽屉导航图标不会替换插入符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31454785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com