使 ActionBar 上的选项卡在达到限制时滚动,而不是创建下拉列表

发布于 2025-01-05 08:10:10 字数 392 浏览 1 评论 0原文

正如标题所示,我有一个 Android 4.0 平板电脑应用程序,它使用操作栏和选项卡模式。

如果我向操作栏添加超过 4 或 5 个选项卡,则会创建一个下拉列表。我已阅读文档,其中指出:“注意:在某些情况下,Android 系统会将操作栏选项卡显示为下拉列表,以确保最适合操作栏。”

只是想知道是否可以覆盖默认行为并让操作栏滚动项目?设计文档 http://developer.android.com/design/patterns/actionbar.html< /a> 谈论可滚动选项卡,但除了设计文档之外,我似乎找不到有关它们的任何信息。

As the title suggests, I have an Android 4.0 Tablet app, that uses the Actionbar and tab mode.

If I add any more than 4 or 5 tabs to the action bar, a dropdown list is created instead. I have read the documentation, which states, "Note: In some cases, the Android system will show your action bar tabs as a drop-down list in order to ensure the best fit in the action bar."

Was just wondering if it is possible to override the default behavior and get the actionbar to scroll the items? The design document http://developer.android.com/design/patterns/actionbar.html talks about scrollable tabs, but I can't seem to find any information on them other than in the design document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫负雪 2025-01-12 08:10:10

我一直在努力让它发挥作用。然而,在使用 ActionBarSherlock 之后,解决方案非常简单。如果您下载示例 Styled 源并查看 MainActivity 类,您可以看到它创建并添加选项卡,并在其下方:

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

我在添加选项卡之前调用了这些选项卡,并且我的选项卡已变成下拉列表。事实证明,您所需要做的就是在添加选项卡后调用它们,而不是保留滚动选项卡。

I have been struggling to get this to work. However, after playing with ActionBarSherlock, the solution is infuriatingly easy. If you download the sample Styled source and look in the MainActivity class, you can see it create and add the tabs, and beneath that:

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

I had called these BEFORE adding the tabs, and my tabs had been turned into a drop-down list. Turns out, all you need to do is call them after adding the tabs instead to keep the scrolling tabs.

赢得她心 2025-01-12 08:10:10

请阅读 http://developer.android 中的“添加操作视图”部分。 com/guide/topics/ui/actionbar.html

操作视图是出现在操作栏中的小部件,作为操作项按钮的替代品。

的引用,将 Horizo​​ntalScrollView 用作操作视图

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mytabwidget"
      android:title=""
      android:icon="@drawable/ic_launcher"
      android:showAsAction="always"
      android:actionViewClass="android.widget.HorizontalScrollView" />
</menu>

您可以通过在 public boolean onCreateOptionsMenu(Menu menu) 中指定获取对此 Horizo​​ntalScrollView

定义一个 RadioGroup< /code> 与一些 RadioButton ,其中每个 RadioButton 当被选中时,将更新其关联的选项卡内容。现在将此 RadioGroup 添加到 Horizo​​ntalScrollView 中,您将在 public boolean onCreateOptionsMenu(Menu menu) 中获得它,

您可以使用 selectTab(ActionBar.Tab tab) 或其他等效方法来更新选项卡内容。

您可以通过将 android:button 设置为透明图像,然后设置一些状态为选中、选中和正常的 StateListDrawable 来更改 RadioButton 的外观。

请参阅我使用此方法得到的下面的屏幕。

在此处输入图像描述

在上图中,每个 RadioButton 选中时会选择不同的选项卡。

Read the "Adding an Action View" section from http://developer.android.com/guide/topics/ui/actionbar.html

An action view is a widget that appears in the action bar as a substitute for an action item's button.

You can use HorizontalScrollView as action view by specifying

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mytabwidget"
      android:title=""
      android:icon="@drawable/ic_launcher"
      android:showAsAction="always"
      android:actionViewClass="android.widget.HorizontalScrollView" />
</menu>

Get reference to this HorizontalScrollView in public boolean onCreateOptionsMenu(Menu menu)

Define a RadioGroup with some RadioButton where each RadioButton when selected, will update its associated tabs contents. Now add this RadioGroup to HorizontalScrollView which you will get in public boolean onCreateOptionsMenu(Menu menu)

You can use selectTab(ActionBar.Tab tab) or some other equivalent method to update tabs contents.

You can change the look of RadioButton by setting android:button to transparent image and then setting some StateListDrawable with states checked, selected and normal..

See below screen I got using this method.

enter image description here

In above image, each RadioButton when checked, selects different tab.

薄荷→糖丶微凉 2025-01-12 08:10:10

您是否检查过 SDK 中的 Honeycomb Gallery 示例应用程序?它可能有您正在寻找的东西。将其安装在 ICS 非平板电脑设备上以查看可滚动选项卡。平板电脑的屏幕空间太大,无法滚动。

http://developer.android.com/resources/samples/HoneycombGallery/index.html

Have you checked out the Honeycomb Gallery sample app in the SDK? It might have what you are looking for. Install it on an ICS non-tablet device to see the scrollable tabs. A tablet has too much screen space for it to scroll.

http://developer.android.com/resources/samples/HoneycombGallery/index.html

沉溺在你眼里的海 2025-01-12 08:10:10

UI 如此工作是有原因的。拥有一组可滚动的选项卡是非常令人困惑的。很可能根本不清楚右侧有更多可用选项卡,并且用户永远不会知道他们可以滚动选项卡以获得更多功能。

我建议坚持平台的设计工作方式。它做事通常都是有原因的。

The UI works the way it is for a reason. It is extremely confusing to have a scrollable set of tabs. There is a good chance that it won't be clear at all that there are more tabs available on the right, and the user will never know they can scroll the tabs to get to more features.

I recommend sticking with how the platform is designed to work. It generally does things for a reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文