Android 更改操作栏中选定的导航项文本

Android 更改操作栏中选定的导航项文本,android,android-actionbar,Android,Android Actionbar,我正在使用ActionBar Sherlock,并且有一个设置了导航模式的ActionBar。我想更改操作栏中显示的当前选定导航项的文本。我已成功更改导航下拉列表中项目的名称。如果通过触摸操作栏中的导航按钮来显示下拉列表,则可以看到这一点。但是,除非我切换到导航下拉列表中的其他索引项,否则操作栏中的文本将不会更改。我尝试了很多方法,例如在操作栏上调用设置选定导航项(),调用无效操作菜单(),等等,但似乎都没有效果 如果有人对如何显示更改后的菜单文本有任何想法,我将不胜感激。您可以通过添加自定义布

我正在使用ActionBar Sherlock,并且有一个设置了导航模式的
ActionBar
。我想更改
操作栏中显示的当前选定导航项的文本。我已成功更改导航下拉列表中项目的名称。如果通过触摸
操作栏中的导航按钮来显示下拉列表,则可以看到这一点。但是,除非我切换到导航下拉列表中的其他索引项,否则
操作栏中的文本将不会更改。我尝试了很多方法,例如在
操作栏上调用
设置选定导航项()
,调用
无效操作菜单()
,等等,但似乎都没有效果


如果有人对如何显示更改后的菜单文本有任何想法,我将不胜感激。

您可以通过添加自定义布局来实现这一点

下面是示例代码

首先定义选项卡的布局

tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <TextView
        android:id="@+id/tabName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#000"
        android:textStyle="bold"
        android:text="TextView" />

</RelativeLayout>
然后在TabListener中,您可以设置所选导航项文本的文本颜色

ActionBar.TabListener tabListener = new ActionBar.TabListener() {


        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            final View tabView = tab.getCustomView();
            TextView text = (TextView) tabView.findViewById(R.id.tabName);
            text.setTextColor(getResources().getColor(android.R.color.black)); //There you have to put your inactive color
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

            final View tabView = tab.getCustomView();
            TextView text = (TextView) tabView.findViewById(R.id.tabName);
            text.setTextColor(getResources().getColor(R.color.bgColor)); //There you have to put your active color


        }
      };

我也有同样的问题!你解决了吗?不幸的是,我不知道怎么做;因此,我最终放弃了这个想法,创建了一个单独的屏幕来管理列表中的项目。
ActionBar.TabListener tabListener = new ActionBar.TabListener() {


        @Override
        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub
            final View tabView = tab.getCustomView();
            TextView text = (TextView) tabView.findViewById(R.id.tabName);
            text.setTextColor(getResources().getColor(android.R.color.black)); //There you have to put your inactive color
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // TODO Auto-generated method stub

            final View tabView = tab.getCustomView();
            TextView text = (TextView) tabView.findViewById(R.id.tabName);
            text.setTextColor(getResources().getColor(R.color.bgColor)); //There you have to put your active color


        }
      };