动作条动态标签文本颜色变化文本、颜色、动作、标签

2023-09-12 03:10:20 作者:还给你的自由

我需要动态改变标签的文本颜色。当选择标签文本应为黄色和未选择时,它应该是黑色的。我尝试了一些招数,但我实现是改变动作条标题颜色...

我的Java code设置选项卡:

 私人无效setSortTabs(){
    //获取来自MainActivity的操作栏
    最后动作条的ActionBar =((ActionBarActivity)getActivity())getSupportActionBar()。
    //添加标签的操作栏
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //选项卡按名称排序
    ActionBar.Tab tabSortByName = actionBar.newTab();
    tabSortByName.setText(LABEL_BY_NAME_DESC);
    ActionBar.TabListener sortByNameTabListener =新ActionBar.TabListener(){
        @覆盖
        公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
            sortByName(标签);
        }

        @覆盖
        公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
            sortByNameTabUnselected(标签);
        }

        @覆盖
        公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
            sortByName(标签);
        }
    };
    tabSortByName.setTabListener(sortByNameTabListener);
    actionBar.addTab(tabSortByName);

    //标签按日期排序
    ActionBar.Tab tabSortByDate = actionBar.newTab();
    tabSortByDate.setText(LABEL_BY_DATE_ASC);
    actionBar.setStackedBackgroundDrawable(新ColorDrawable(Color.BLUE));
    ActionBar.TabListener sortByDateTabListener =新ActionBar.TabListener(){
        @覆盖
        公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
            sortByDate(标签);
            。//Toast.makeText(getActivity()getApplicationContext(),Integer.toString(tab.getPosition()),1).show();
        }

        @覆盖
        公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
            sortByDateTabUnselected(标签);

        }

        @覆盖
        公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction){
            sortByDate(标签);
        }
    };
    tabSortByDate.setTabListener(sortByDateTabListener);
    actionBar.addTab(tabSortByDate);
}
 

我的styles.xml code:

 <样式名称=AppTheme
    父=Theme.Base.AppCompat.Light>
    <项目名称=机器人:actionBarStyle工具:忽略=NewApi> @风格/ MyActionBar< /项目>
    <项目名称=机器人:actionBarTabTextStyle> @风格/ TabTextStyle< /项目>
    <项目名称=机器人:actionBarTabStyle> @风格/ TabBackgroundStyle< /项目>
    <项目名称=机器人:actionBarTabBarStyle> @风格/ TabBarBackgroundStyle< /项目>

    <! - 支持库的兼容性 - >
    <项目名称=actionBarStyle> @风格/ MyActionBar< /项目>
    <项目名称=actionBarTabTextStyle> @风格/ TabTextStyle< /项目>
    <项目名称=actionBarTabStyle> @风格/ TabBackgroundStyle< /项目>
    <项目名称=actionBarTabBarStyle> @风格/ TabBarBackgroundStyle< /项目>
< /风格>

<! - 动作条的风格 - >
<样式名称=MyActionBar
    父=@风格/ Widget.AppCompat.Light.ActionBar>
    <项目名称=机器人:背景> @色/ actionBarBackgroundColor< /项目>
    <项目名称=机器人:图标> @android:彩色/透明< /项目>

    <! - 支持库的兼容性 - >
    <项目名称=背景> @色/ actionBarBackgroundColor< /项目>
    <项目名称=图标> @android:彩色/透明< /项目>
< /风格>

<样式名称=TabTextStyle
    父=@风格/ Widget.AppCompat.ActionBar.TabText>
    <项目名称=机器人:文字颜色> @色/ selected_tab_text_color< /项目>
    <项目名称=机器人:TEXTSIZE> 20SP< /项目>
< /风格>

<样式名称=TabBackgroundStyle
    父=@风格/ Widget.AppCompat.ActionBar.TabView>
    <项目名称=机器人:背景> @色/ gray_edit_text< /项目>
    <项目名称=安卓重力>中心< /项目>
< /风格>

<样式名称=TabBarBackgroundStyle
    父=@风格/ Widget.AppCompat.ActionBar.TabBar>
    <项目名称=机器人:背景> @色/ gray_edit_text< /项目>
< /风格>
 

目前看VS所需的外观

解决方案

我对我的项目同样的要求。下面code ++工程,通过为每个选项卡,通过它可以根据需要设定/更改文本颜色(或做其他格式更改)自定义视图。无论选择哪个选项卡将有黄色文字,未选中的选项卡将有黑色文本。

 公共无效createTabs(){
    //硬编码为测试目的...
    串tabTitles [] = {TAB0,TAB1,TAB2};
    INT numTabs = 3;
    INT startingSelectedTab = 0;

    的for(int i = 0; I< numTabs;我++){
        布尔选择=(我== startingSelectedTab);

        TAB键= mActionBar.newTab();
        tab.setTabListener(本);

        查看查看= createTabCustomView(tabTitles [我],选择);
        tab.setCustomView(视图);

        mActionBar.addTab(选项卡,选择);
    }
}

公共查看createTabCustomView(字符串名称,布尔isSelected){
    // XML文件低于
    RelativeLayout的rootView =(RelativeLayout的)getLayoutInflater()膨胀(R.layout.action_bar_custom_tab,空)。

    TextView中的TextView = setTextColor(rootView,isSelected);
    textView.setText(职称);

    返回rootView;
}

公共查看setTabTextColor(TAB键,布尔isSelected){
    //获取认为是在createTabCustomView创建()
    RelativeLayout的rootView =(RelativeLayout的)tab.getCustomView();

    setTextColor(rootView,isSelected);

    返回rootView;
}

公众的TextView setTextColor(查看rootView,布尔isTabSelected){
    TextView中的TextView =(TextView中)rootView.findViewById(R.id.custom_tab_textview);

    如果(isTabSelected){
        textView.setTextColor(Color.YELLOW);
    } 其他 {
        textView.setTextColor(Color.BLACK);
    }
    返回的TextView;
}

@覆盖
//实现ActionBar.TabListener
公共无效onTabSelected(TAB键,android.app.FragmentTransaction英尺){
    查看查看= setTabTextColor(标签,真正的);
    tab.setCustomView(视图);
}

@覆盖
//实现ActionBar.TabListener
公共无效onTabUnselected(TAB键,android.app.FragmentTransaction英尺){
    查看查看= setTabTextColor(标签,假);
    tab.setCustomView(视图);
}

@覆盖
//实现ActionBar.TabListener
公共无效onTabReselected(TAB键,android.app.FragmentTransaction英尺){
}
 
AE教程 超酷的故障风动态LOGO

action_bar_custom_tab.xml:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_margin =0dp
    机器人:填充=0dp>

    <的TextView
        机器人:ID =@ + ID / custom_tab_textview
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_centerHorizo​​ntal =真
        机器人:layout_centerInParent =真
        机器人:重力=中心| center_horizo​​ntal
        机器人:TEXTSTYLE =黑体/>

< / RelativeLayout的>
 

I need to change the tab text color dynamically. When the tab is selected text should be yellow and when unselected it should be black. I tried some tricks, but all I achieved was to change the ActionBar title color...

My Java code setting up the tabs:

     private void setSortTabs() {
    //getting the action bar from the MainActivity
    final ActionBar actionBar = ((ActionBarActivity) getActivity()).getSupportActionBar();
    //adding tabs to the action bar
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    //tab sort by name
    ActionBar.Tab tabSortByName = actionBar.newTab();
    tabSortByName.setText(LABEL_BY_NAME_DESC);
    ActionBar.TabListener sortByNameTabListener = new ActionBar.TabListener() {
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            sortByName(tab);
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            sortByNameTabUnselected(tab);
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            sortByName(tab);
        }
    };
    tabSortByName.setTabListener(sortByNameTabListener);
    actionBar.addTab(tabSortByName);

    //tab sort by date
    ActionBar.Tab tabSortByDate = actionBar.newTab();
    tabSortByDate.setText(LABEL_BY_DATE_ASC);
    actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.BLUE));
    ActionBar.TabListener sortByDateTabListener = new ActionBar.TabListener() {
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            sortByDate(tab);
            //Toast.makeText(getActivity().getApplicationContext(),Integer.toString(tab.getPosition()), 1).show();
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            sortByDateTabUnselected(tab);

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
            sortByDate(tab);
        }
    };
    tabSortByDate.setTabListener(sortByDateTabListener);
    actionBar.addTab(tabSortByDate);
}

My styles.xml code:

    <style name="AppTheme"
    parent="Theme.Base.AppCompat.Light">
    <item name="android:actionBarStyle" tools:ignore="NewApi">@style/MyActionBar</item>
    <item name="android:actionBarTabTextStyle">@style/TabTextStyle</item>
    <item name="android:actionBarTabStyle">@style/TabBackgroundStyle</item>
    <item name="android:actionBarTabBarStyle">@style/TabBarBackgroundStyle</item>

    <!-- Support library compatibility -->
    <item name="actionBarStyle">@style/MyActionBar</item>
    <item name="actionBarTabTextStyle">@style/TabTextStyle</item>
    <item name="actionBarTabStyle">@style/TabBackgroundStyle</item>
    <item name="actionBarTabBarStyle">@style/TabBarBackgroundStyle</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/actionBarBackgroundColor</item>
    <item name="android:icon">@android:color/transparent</item>

    <!-- Support library compatibility -->
    <item name="background">@color/actionBarBackgroundColor</item>
    <item name="icon">@android:color/transparent</item>
</style>

<style name="TabTextStyle"
    parent="@style/Widget.AppCompat.ActionBar.TabText">
    <item name="android:textColor">@color/selected_tab_text_color</item>
    <item name="android:textSize">20sp</item>
</style>

<style name="TabBackgroundStyle"
    parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@color/gray_edit_text</item>
    <item name="android:gravity">center</item>
</style>

<style name="TabBarBackgroundStyle"
    parent="@style/Widget.AppCompat.ActionBar.TabBar">
    <item name="android:background">@color/gray_edit_text</item>
</style>

Current look vs desired look

解决方案

I had the same requirement for my project. The following code works by creating a custom view for each tab, with which you can set/change text color (or do other formatting changes) as desired. Whichever tab is selected will have yellow text, the unselected tabs will have black text.

public void createTabs() {
    // Hardcoding for test purposes...
    String tabTitles[] = {"Tab0", "Tab1", "Tab2"};
    int numTabs = 3;
    int startingSelectedTab = 0;

    for (int i = 0; i < numTabs; i++) {             
        Boolean selected = (i == startingSelectedTab);

        Tab tab = mActionBar.newTab();
        tab.setTabListener(this);

        View view = createTabCustomView(tabTitles[i], selected);
        tab.setCustomView(view);

        mActionBar.addTab(tab, selected);
    }
}

public View createTabCustomView(String title, boolean isSelected)  {
    // XML file is below  
    RelativeLayout rootView = (RelativeLayout) getLayoutInflater().inflate(R.layout.action_bar_custom_tab, null);

    TextView textView = setTextColor(rootView, isSelected);
    textView.setText(title);

    return rootView;
}

public View setTabTextColor(Tab tab, boolean isSelected) {
    // Get view that was created in createTabCustomView()
    RelativeLayout rootView = (RelativeLayout) tab.getCustomView();

    setTextColor(rootView, isSelected);

    return rootView;
}

public TextView setTextColor(View rootView, boolean isTabSelected) {
    TextView textView = (TextView) rootView.findViewById(R.id.custom_tab_textview);

    if (isTabSelected) {
        textView.setTextColor(Color.YELLOW);
    } else {
        textView.setTextColor(Color.BLACK);
    }
    return textView;
}

@Override
// Implement ActionBar.TabListener 
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
    View view = setTabTextColor(tab, true);
    tab.setCustomView(view);
}

@Override
// Implement ActionBar.TabListener 
public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {
    View view = setTabTextColor(tab, false);
    tab.setCustomView(view);
}

@Override
// Implement ActionBar.TabListener 
public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {      
}   

action_bar_custom_tab.xml:

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

    <TextView
        android:id="@+id/custom_tab_textview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:gravity="center|center_horizontal"
        android:textStyle="bold"/>

</RelativeLayout>