0

我正在使用 FragmentTabHost 将框架保存为标签。我想删除蓝色下划线吹标签。这个怎么做。

在此处输入图像描述

public  class MainMenuFragment extends Fragment {

        private FragmentTabHost mTabHost; 
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_menu, container, false);


            // Locate android.R.id.tabhost in main_fragment.xml
          mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);

            // Create the tabs in main_fragment.xml
            mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabcontent);

            // Create Tab1 with a custom image in res folder
           mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Appetizers"),
                    AppetizersMenuFragment.class, null);  

        // Create Tab2
            mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Entrees"),
                    EntreesMenuFragment.class, null);  

            // Create Tab2
            mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Desserts"),
                    DessertsMenuFragment.class, null);   


            return rootView;
        }
    }
4

1 回答 1

4

选项卡指示器的颜色由选择器可绘制设置,可在此处找到,如下所示:

<!-- AOSP copyright notice can be found at the above link -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_holo" />

    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected_focused_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/tab_selected_focused_holo" />

    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />

    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/tab_unselected_pressed_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/tab_selected_pressed_holo" />
</selector>

选择器使用的可绘制对象都以浅蓝色着色。您可以用自己重新着色的版本替换这些可绘制对象。原件看起来像这样(原件很小,包括链接):

您需要将上述选择器与可绘制对象一起复制到您自己的项目中。然后,您需要将可绘制对象重新着色为您想要的任何颜色。然后,您需要将选择器设置为选项卡指示器的背景。您可以这样做(设置标签后):

TabHost host = (TabHost)view.findViewById(R.id.tab_host);
TabWidget widget = host.getTabWidget();
for(int i = 0; i < widget.getChildCount(); i++) {
    View v = widget.getChildAt(i);

    // Look for the title view to ensure this is an indicator and not a divider.
    TextView tv = (TextView)v.findViewById(android.R.id.title);
    if(tv == null) {
        continue;
    }
    v.setBackgroundResource(R.drawable.your_tab_selector_drawable);
}

通过使用背景选择器设置您自己的客户指示器布局可能有一种更简单的方法,但这对我来说最简单。

于 2013-11-03T07:22:22.850 回答