自定义下划线在动作栏sherlock android

时间:2013-08-23 05:55:30

标签: android

我在我的应用程序中使用android操作栏sherlock。 我希望我的动作栏看起来像这样。

enter image description here

但是当我改变动作条标签的背景颜色时,下划线会消失。

这是我的代码。

<style name="MyTheme" parent="Theme.Sherlock.Light">
<!-- define text style for tabs -->
<item name="actionBarTabTextStyle">@style/MyTabTextStyle</item>
<item name="android:actionBarTabTextStyle">@style/MyTabTextStyle</item>

<item name = "actionBarStyle">@style/MyBarStyle</item>
<item name = "android:actionBarStyle">@style/MyBarStyle</item>

<item name = "actionBarTabStyle">@style/MyTabStyle</item>
<item name = "android:actionBarTabStyle">@style/MyTabStyle</item>

<item name = "actionBarTabBarStyle">@style/MyTabBarStyle</item>
<item name = "android:actionBarTabBarStyle">@style/MyTabBarStyle</item>


</style>

    <style name="MyTabTextStyle" parent="Widget.Sherlock.ActionBar.TabText" >
<item name="android:textColor">@color/tab_text_indicator</item>
<item name= "android:textSize">15dp</item>
<item name= "android:textStyle">normal</item>
</style>

    <style name="MyBarStyle" parent="Widget.Sherlock.ActionBar" >
   <item name="android:textSize">18dp</item>
   <item name="android:textStyle">normal</item>
   <item name="android:background">#669933</item>
    <item name="background">#669933</item>
    </style>

    <style name="MyTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabView" >
   <item name="android:background">#ffffff</item>
    <item name="background">#ffffff</item> 
    </style>

    <style name="MyTabBarStyle" parent="Widget.Sherlock.ActionBar.TabBar" >
     <item name="android:showDividers">none</item>
   </style>

,输出

enter image description here

所以,只是不知道如何在我的动作标签中设置下划线。请帮助.. 感谢。

2 个答案:

答案 0 :(得分:0)

试试这段代码:

  import android.view.View;
    import android.view.animation.AccelerateInterpolator;
    import android.view.animation.Animation;
    import android.view.animation.TranslateAnimation;
    import android.widget.TabHost;
    import android.widget.TabHost.OnTabChangeListener;

/**
 * A custom OnTabChangeListener that uses the TabHost its related to to fetch information about the current and previous
 * tabs. It uses this information to perform some custom animations that slide the tabs in and out from left and right.
 * 
 * @author Daniel Kvist
 * 
 */
public class AnimatedTabHostListener implements OnTabChangeListener
{

    private static final int ANIMATION_TIME = 240;
    private TabHost tabHost;
    private View previousView;
    private View currentView;
    private int currentTab;

    /**
     * Constructor that takes the TabHost as a parameter and sets previousView to the currentView at instantiation
     * 
     * @param tabHost
     */
    public AnimatedTabHostListener(TabHost tabHost)
    {
        this.tabHost = tabHost;
        this.previousView = tabHost.getCurrentView();
    }

    /**
     * When tabs change we fetch the current view that we are animating to and animate it and the previous view in the
     * appropriate directions.
     */
    @Override
    public void onTabChanged(String tabId)
    {

        currentView = tabHost.getCurrentView();
        if (tabHost.getCurrentTab() > currentTab)
        {
            previousView.setAnimation(outToLeftAnimation());
            currentView.setAnimation(inFromRightAnimation());
        }
        else
        {
            previousView.setAnimation(outToRightAnimation());
            currentView.setAnimation(inFromLeftAnimation());
        }
        previousView = currentView;
        currentTab = tabHost.getCurrentTab();

    }

    /**
     * Custom animation that animates in from right
     * 
     * @return Animation the Animation object
     */
    private Animation inFromRightAnimation()
    {
        Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        return setProperties(inFromRight);
    }

    /**
     * Custom animation that animates out to the right
     * 
     * @return Animation the Animation object
     */
    private Animation outToRightAnimation()
    {
        Animation outToRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        return setProperties(outToRight);
    }

    /**
     * Custom animation that animates in from left
     * 
     * @return Animation the Animation object
     */
    private Animation inFromLeftAnimation()
    {
        Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        return setProperties(inFromLeft);
    }

    /**
     * Custom animation that animates out to the left
     * 
     * @return Animation the Animation object
     */
    private Animation outToLeftAnimation()
    {
        Animation outtoLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
        return setProperties(outtoLeft);
    }

    /**
     * Helper method that sets some common properties
     * @param animation the animation to give common properties
     * @return the animation with common properties
     */
    private Animation setProperties(Animation animation)
    {
        animation.setDuration(ANIMATION_TIME);
        animation.setInterpolator(new AccelerateInterpolator());
        return animation;
    }
}

答案 1 :(得分:0)

如果您正在寻找像GooglePlayStore应用这样的tabnavigation,那么PagerTabStripViewPager会很容易。

以下是您AndroidSolution4U

的好例子

在本教程中,您可以通过更改以下代码中的颜色来自定义选项卡

 <android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#676966"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#6CBD57" />