gpt4 book ai didi

android - 仅使用标题切换 DrawerLayout(无应用程序图标)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:33:00 25 4
gpt4 key购买 nike

我有一个使用 DrawerLayout 的 Activity 。我可以用两种不同的方式打开抽屉……从屏幕的左侧区域向右滑动,然后单击应用程序标题。我没有显示应用图标,只有标题。我完全按照谷歌的建议实现了这个:Creating a Navigation Drawer: Open and Close with the App Icon

就打开和关闭抽屉本身而言,一切都是正常的。但是,它不显示应该使用的标准 DrawerLayout 图标。相反,我得到了常规的向上插入符(看起来像一个小于号)。

只要我将应用程序图标添加回 ActionBar,它就会按预期开始工作。当抽屉打开或关闭时,抽屉布局图标会显示并显示动画。我已尝试在我的样式 XML 文件中和以编程方式删除应用程序图标。

有没有办法让 DrawerLayout 图标在没有应用程序图标的情况下工作???

更新:我找到了变通方法,但与其说是解决方案,不如说是一种技巧。我只是创建了一个 1x1 像素的透明 PNG (blank.png) 并将其设置为我的 styles.xml 文件中的应用程序图标。以下是所有相关代码:

styles.xml

<style name="MyCustomTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyCustomActionBar</item>
<item name="android:icon">@drawable/blank</item>
</style>

<style name="MyCustomActionBar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>

MainActivity -> onCreate()

this.navDrawerToggle = new ActionBarDrawerToggle
(
this,
this.navDrawerLayout,
R.drawable.icon_nav_drawer,
R.string.nav_drawer_open,
R.string.nav_drawer_closed
)
{
public void onDrawerClosed(View view) {}
public void onDrawerOpened(View drawerView) {}
};

MainActivity -> onPostCreate()

super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();

MainActivity -> onResume()

this.navDrawer.setOnItemClickListener(new DrawerItemClickListener());
this.navDrawerLayout.setDrawerListener(this.navDrawerToggle);

MainActivity -> onPause()

this.navDrawer.setOnItemClickListener(null);
this.navDrawerLayout.setDrawerListener(null);

MainActivity -> onConfigurationChanged(Configuration newConfig)

super.onConfigurationChanged(newConfig);
navDrawerToggle.onConfigurationChanged(newConfig);

MainActivity -> onOptionsItemSelected(菜单项)

if (this.navDrawerToggle.onOptionsItemSelected(item)) {return true;}
else
{
// A bunch of item click handling happens here...

return true;
}

最佳答案

我对此很好奇,所以我尝试了 DrawerLayout 的示例,它运行良好。此外,似乎您必须使用自己的可绘制抽屉图标,无论如何推荐。您可以从此站点下载默认 Assets Creating a Navigation Drawer并将它们放入各自的可绘制资源文件夹中。

这是对我有用的:

ActionBar actionBar = getActionBar();

// Let's get rid of the app icon here
actionBar.setIcon(null);
actionBar.setTitle("App Name");

// Setting these 3 options allows us to show the title as well as a "Home" elements
// "Home" elements include the Up and/or Drawer icon. The DISPLAY_HOME_AS_UP will enable
// and show the Drawer icon, we'll be "replacing" the "up" button with the drawer icon below
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE
| ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP);

// Get a reference of the DrawerLayout
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(drawerToggle);

// Setting ActionBarDrawerToggle will allow you to set the drawables for the drawer
// (this will also give you the nice/smooth animation) as well as allow you to do some
// other things depending on the events: onDrawerClosed & onDrawerOpened.
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_closed /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
actionBar.setTitle("Closed...");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}

public void onDrawerOpened(View drawerView) {
actionBar.setTitle("Open...");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};

// Set a listener to be notified of drawer events.
drawerLayout.setDrawerListener(drawerToggle);

更新: 似乎没有考虑 ActionBar 样式上的 android:displayOptions。请改用 actionBar.setDisplayOptions(int options)。

关于android - 仅使用标题切换 DrawerLayout(无应用程序图标)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17161301/

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