删除片段选项卡下方的蓝色下划线

时间:2013-11-03 07:03:05

标签: android fragment-tab-host

我使用FragmentTabHost将片段作为标签保存。我想删除蓝色下划线标签。如何做到这一点。

enter image description here

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;
        }
    }

1 个答案:

答案 0 :(得分:4)

选项卡指示器的颜色由选择器drawable设置,可以找到here,如下所示:

<!-- 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>

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

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

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);
}

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

相关问题