【发布时间】:2011-11-13 10:12:26
【问题描述】:
我想在我的应用程序顶部有一个菜单栏 - 就像 Facebook、Google+ 或 Twitter 一样:
这是来自 Twitter 应用程序的屏幕截图,其中显示了此栏:它显示在每个活动中,左侧有公司徽标(可点击),右侧有 1-3 个菜单项(可点击图片)。
在 GDDatalog 应用中也可以看到:
因此,该操作栏有一些要求:
- 它也必须在较旧的 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's solution 是不是最好的?
提前非常感谢!
【问题讨论】:
-
最好和最简单的是主观的。你为什么不尝试一些东西,当你遇到特定问题时再回来。
-
“最佳实践”绝不是主观的。它始终是一些开发人员认为的最佳解决方案。如果我向这里的每个开发人员询问他或她喜欢的解决方案,我们就会知道什么是最好的解决方案。当然我不想知道有人喜欢哪种解决方案,我们都想知道原因和论据。我有一个特定的问题:我不知道应该如何实现该操作栏。 ;)
标签: android user-interface navigation titlebar