如何使用 appcompat-v7 22.1 获取样式为对话框的活动的操作栏?
How to get actionbar for activities styled as dialog with appcompat-v7 22.1?
一些背景知识:我正在为一款以前在手机上运行了很长时间的游戏添加平板电脑支持。一些活动构成了一个“向导”,用户可以在其中选择游戏类型、一些选项和游戏开始前的对手。对于为这些活动使用继承 Theme.AppCompat.DialogWhenLarge
的主题的平板电脑版本,看起来是一个很好、简单的解决方案。这在使用 appcompat-v7 版本 22.0.0 时运行良好。
在 appcompat-v7 版本 22.1 中,在所有这些 DialogWhenLarge 活动中,突然 getSupportActionBar()
开始在平板电脑上返回 null
。我依靠操作栏进行后退导航,更重要的是用于搜索和其他按钮。
在平板电脑上获得这些活动的操作栏的合适方法是什么?我需要实现自己的工具栏吗?
我试过在我的 onCreate
中调用 requestWindowFeature(Window.FEATURE_ACTION_BAR)
(在调用 super 之前),但这没有效果。我尝试从 AppCompatActivity
而不是 ActionBarActivity
进行子类化,但这也无济于事(无论是否有 requestWindowFeature)。
我已阅读 Android 开发者博客上的 Chris Banes' blog post on this new library version as well as Ian Lake's post;我看不到任何提及操作栏不再可用的内容。
我遇到了同样的问题。我能找到的唯一修复方法是用工具栏替换操作栏。
// Replace the action bar with a toolbar
Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
我的工具栏布局:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/DefaultActionBarTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
包含在我的 activity 中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detail_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/tool_bar" layout="@layout/tool_bar" />
<ListView
android:id="@+id/detail_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="always" />
</LinearLayout>
DefaultActionBarTheme(在 styles.xml 中):
<!-- Action bar theme -->
<style name="DefaultActionBarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlNormal">#1d7caf</item>
</style>
一些背景知识:我正在为一款以前在手机上运行了很长时间的游戏添加平板电脑支持。一些活动构成了一个“向导”,用户可以在其中选择游戏类型、一些选项和游戏开始前的对手。对于为这些活动使用继承 Theme.AppCompat.DialogWhenLarge
的主题的平板电脑版本,看起来是一个很好、简单的解决方案。这在使用 appcompat-v7 版本 22.0.0 时运行良好。
在 appcompat-v7 版本 22.1 中,在所有这些 DialogWhenLarge 活动中,突然 getSupportActionBar()
开始在平板电脑上返回 null
。我依靠操作栏进行后退导航,更重要的是用于搜索和其他按钮。
在平板电脑上获得这些活动的操作栏的合适方法是什么?我需要实现自己的工具栏吗?
我试过在我的 onCreate
中调用 requestWindowFeature(Window.FEATURE_ACTION_BAR)
(在调用 super 之前),但这没有效果。我尝试从 AppCompatActivity
而不是 ActionBarActivity
进行子类化,但这也无济于事(无论是否有 requestWindowFeature)。
我已阅读 Android 开发者博客上的 Chris Banes' blog post on this new library version as well as Ian Lake's post;我看不到任何提及操作栏不再可用的内容。
我遇到了同样的问题。我能找到的唯一修复方法是用工具栏替换操作栏。
// Replace the action bar with a toolbar
Toolbar toolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
我的工具栏布局:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/DefaultActionBarTheme"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
包含在我的 activity 中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detail_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="@+id/tool_bar" layout="@layout/tool_bar" />
<ListView
android:id="@+id/detail_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="always" />
</LinearLayout>
DefaultActionBarTheme(在 styles.xml 中):
<!-- Action bar theme -->
<style name="DefaultActionBarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorControlNormal">#1d7caf</item>
</style>