我正在使用 ActionBarSherlock 并且想要一个与 Google map 已知的主页按钮类似的功能。
使用这段代码按下主页按钮时,我已经设法打开了一个 QuickAction ( http://code.google.com/p/simple-quickactions/):
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
View anchor = findViewById(R.id.text); // how do I get the home button view here?
quickAction.show(anchor);
return true;
}
return false;
}
但是我使用 Activity 的简单 TextView 作为 anchor (R.id.text)。如何从那里的 ActionBar 检索主页按钮 View ?
哦,findViewById(android.R.id.home)
或 findViewById(item.getItemId())
都返回 null。
最佳答案
您无法访问主页 View ,因为它位于内容 View 上方和窗口装饰中。
Google 在存在于内容 View 中的 Honeycomb 之前的设备上使用自定义操作栏。这就是允许他们制作此自定义下拉菜单的原因。在 Honeycomb 和更新版本上,他们在系统操作栏中使用高度自定义的列表导航版本。
由于您使用的是 ActionBarSherlock,因此您有两个选择:
- 选择更新样式的导航并自定义内置列表导航。
- 提供模仿主页 View 的自定义导航 View (例如,其中包含您的应用程序图标)。使用此自定义 View ,您将能够提供一个可以附加自定义弹出菜单的 anchor 。
关于android - ActionBarSherlock:如何访问主页按钮 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10187523/