我希望在我的应用程序顶部有一个菜单栏——就像Facebook、Google+或Twitter一样:
这是Twitter应用程序的屏幕截图,显示了该栏:它在每个活动中显示,在左侧有公司标志(可点击),右侧有1-3个菜单项(可点击图片)。
它也可以在GDCatalog应用程序中看到:
因此,对于该操作栏,有几个要求:
- 它必须在较旧的Android平台上运行,例如API级别8。
- 它必须在每个活动中都可用,而不必一次又一次地重复代码。
- 它必须适应屏幕大小,以使其占据整个宽度。
如何最佳实践地实现这样的操作栏?
GreenDroid将其做成了这样(使用合并):
<?xml version="1.0" encoding="utf-8"?>
<!--
/*
** Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
-->
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
style="?attr/gdActionBarItemStyle"
android:id="@+id/gd_action_bar_item"
android:layout_height="fill_parent"
android:scaleType="center" />
<ImageView
android:layout_width="?attr/gdActionBarDividerWidth"
android:layout_height="fill_parent"
android:background="?attr/gdActionBarDividerDrawable" />
<TextView
style="?attr/gdActionBarTitleStyle"
android:id="@+id/gd_action_bar_title"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:gravity="left|center_vertical"
android:singleLine="true"
android:textColor="?attr/gdActionBarTitleColor"
android:textSize="16sp"
android:textStyle="bold"
android:paddingRight="10dp"
android:paddingLeft="10dp" />
</merge>
这是一个好的解决方案吗?它也适用于旧平台吗?这里还缺少哪些编码部分?我知道我们可以在这里找到一些关于操作栏的问题。但尽管如此,我仍然无法找到实现菜单栏在(几乎)所有API级别上工作的最佳且最简单的方法。也许Google的解决方案是最好的?
提前非常感谢!