java 在 Android 4 上自定义 ActionBar 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10272812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:20:01  来源:igfitidea点击:

Customizing ActionBar Tabs on Android 4

javaandroidandroid-layoutandroid-themeandroid-actionbar

提问by Peter O.

I'm trying to customize Tabs on my ActionBar. I just want to align tabs to phone screen and make it stretchable for various screens. But all I get is this:

我正在尝试自定义 ActionBar 上的选项卡。我只想将选项卡与手机屏幕对齐并使其可拉伸以适应各种屏幕。但我得到的只是这个:

enter image description here

在此处输入图片说明

The code I use for the theme is this:

我用于主题的代码是这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabTextStyle</item>
    </style>

    <style name="MyActionBarTabTextStyle" parent="@android:style/Widget.Holo.Light.Tab">
        <item name="android:textSize">14dip</item>
        <item name="android:padding">0dip</item>
    </style>

</resources>

Is it possible just decrease the font and get rid off those blue lines?

是否有可能只是减少字体并摆脱那些蓝线?

Thanks for help in advance.

提前感谢您的帮助。

P.S.: Also the Button acts weird. The is no text (while it should be)...

PS:按钮的行为也很奇怪。没有文字(虽然应该是)...

采纳答案by Alex Lockwood

Edit:Use ActionBarCompatin the support library.

编辑:使用ActionBarCompat在支持库。



I suggest you use Jake Wharton's ActionBarSherlockfor two reasons:

我建议你使用 Jake Wharton 的ActionBarSherlock有两个原因:

  1. ActionBarSherlock is an extension of the compatibility library that facilitates the use of the action bar across all versions of Android. This means users running your application on pre-HoneyComb versions of Android will be able to use the ActionBarfor navigation as well. This is preferable to both the user (since the ActionBaris awesome!) and to you (since you don't have to worry about separating the control flow of your application based on whether or not the ActionBarwill be available. For instance, without a pre-HoneyComb compatible ActionBar, you might have to worry about creating multiple Activitys for a single screen... one that is optimized for usage with the ActionBar, and one that can be run on pre-HoneyComb devices.

  2. With ActionBarSherlock, stylingyour ActionBar is easy! I could go into detail on how to do it, but the library already comes with a LOT of sample codethat illustrates how to what you are describing above.

  1. ActionBarSherlock 是兼容性库的扩展,它有助于在所有 Android 版本中使用操作栏。这意味着在HoneyComb 之前版本的Android 上运行您的应用程序的用户也可以使用ActionBar导航。这对用户(因为ActionBar太棒了!)和你(因为你不必担心根据是否ActionBar可用来分离应用程序的控制流。例如,没有预-HoneyComb 兼容ActionBar,您可能不得不担心Activity为单个屏幕创建多个s...一个针对与ActionBar.

  2. 使用 ActionBarSherlock,为您的 ActionBar 设置样式很容易!我可以详细说明如何做到这一点,但该库已经附带了大量示例代码,这些示例代码说明了如何实现您在上面描述的内容。

Installing/making use of the library is very easy and is almost identical to making use of the normal ActionBarprovided in the Android SDK (mostly just call getSupportActionBar()instead of getActionBar()). If you have any questions, let me know.

安装/使用该库非常简单,几乎与使用ActionBarAndroid SDK 中提供的普通方法相同(主要只是调用getSupportActionBar()而不是getActionBar())。如果您有任何问题,请告诉我。

p.s. Also note that ActionBarSherlockprovides all of the functionalities that the ViewPagerExtensions library provides and more. I would definitely recommend using the former over the latter.

ps 另请注意,它ActionBarSherlock提供了 ViewPagerExtensions 库提供的所有功能等等。我绝对会推荐使用前者而不是后者。

回答by galex

There's a brand new tool to generate the correct 9patch images for the actionbar : http://jgilfelt.github.com/android-actionbarstylegenerator/

有一个全新的工具可以为操作栏生成正确的 9patch 图像:http://jgilfelt.github.com/android-actionbarstylegenerator/

回答by draksia

I have found using style guides for the tabs to very inconsistent for honeycomb, It was much easier to just inflate a custom view that does want you want.

我发现使用选项卡的样式指南对于蜂窝非常不一致,只是膨胀一个你想要的自定义视图要容易得多。

Fragment serivceFrag = new ServicesFragment();
Tab serviceTab = bar.newTab();
TextViewPlus serviceTabTextView =(TextViewPlus)getLayoutInflater().inflate(R.layout.tab_item_layout, null);
serviceTabTextView.setText(servicesLabel);
serviceTab.setCustomView(serviceTabTextView);
serviceTab.setTabListener(new TabListenerFragment(serivceFrag,"Service",R.id.MainInfoPaneFragment));
bar.addTab(serviceTab);

This to me was much simpler,

这对我来说要简单得多,

回答by Rafael

Change to:

改成:

<style name="MyActionBarTabTextStyle" parent="@android:style/Widget.Holo.ActionBar.TabText">
     <item name="android:textSize">14dip</item>
     <item name="android:padding">0dip</item>
</style>

回答by marmor

I think you're inheriting from the wrong style, try:

我认为您继承了错误的风格,请尝试:

<style name="MyActionBarTabTextStyle" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textSize">14sp</item>
    <item name="android:padding">0dip</item>
</style>

EDIT: also, make sure you use sp instead of dip for font size (14sp)

编辑:另外,请确保您使用 sp 而不是 dip 来表示字体大小(14sp)

回答by Marckaraujo

I strongly recommend that you use ViewPagerExtensions library, it give you 5 different styled tabs to you choose from, also the problem that you have to fill tabs in portrait and landscape ,ode for each screen size is already solved in this library.

我强烈建议您使用ViewPagerExtensions 库,它为您提供 5 种不同样式的选项卡供您选择,此外,您必须以纵向和横向填充选项卡的问题,每个屏幕尺寸的 ode 都已在此库中解决。

By the way, if you look inside the library′s code, you can easy theme this yourself.

顺便说一句,如果您查看库的代码,您可以自己轻松地设置主题。

Also, if you have trouble implementing this with fragment page, look actionbarsherlock-tabs-multi-fragments

此外,如果您在片段页面中无法实现此功能,请查看actionbarsherlock-tabs-multi-fragments

回答by ademar111190

You can use the setCustomViewmethod.

您可以使用setCustomView方法。