【问题标题】:Is it possible to use ActionBarSherlock style on native TabWidget?是否可以在原生 TabWidget 上使用 ActionBarSherlock 样式?
【发布时间】:2012-11-03 16:34:11
【问题描述】:

我想知道是否可以在原生 TabWidget 上应用 ActionBarSherlock (v4.2.0) TabWidget 主题?所以它看起来像前 ICS 机器人上的 Holo。如果你知道如何 - 请分享。

我发现的只有这个:https://gist.github.com/1126843,但它似乎不再起作用了。

标准 xml 布局:

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

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

【问题讨论】:

    标签: android styles actionbarsherlock tabwidget


    【解决方案1】:

    我自己找到了一些解决方案。这不是纯粹的 ABS 资源,而是使用最少的额外资源:

    外几乎与 ABS 选择器相同
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/abs__ab_transparent_light_holo" />
    

    tab_indicator_compat_holo.xml

    <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/abs__ab_transparent_light_holo" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/abs__tab_selected_holo" />
    
    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/abs__list_focused_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/abs__tab_selected_focused_holo" />
    
    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/abs__list_pressed_holo_dark" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/abs__tab_selected_pressed_holo" />
    
    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/abs__tab_unselected_pressed_holo" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/abs__tab_selected_pressed_holo" />
    

    活动

    private void initTabs(){
        TabHost tabHost = (TabHost)getView().findViewById(android.R.id.tabhost);
        tabHost.setup();
    
        TabSpec podcasts_all = tabHost.newTabSpec("tab1");
        TabSpec podcasts_downloaded = tabHost.newTabSpec("tab2");
        TabSpec podcasts_favorite = tabHost.newTabSpec("tab3");
    
    
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            //set divider drawable
    
            tabHost.getTabWidget().setDividerDrawable(com.actionbarsherlock.R.drawable.abs__list_divider_holo_light);
    
            //set tab titles with custom view
            podcasts_all.setIndicator(customTabTextView(getSherlockActivity().getString(R.string.PodcastManager_All_Title)));
            podcasts_downloaded.setIndicator(customTabTextView(getSherlockActivity().getString(
                    R.string.PodcastManager_Downloaded_Title)));
            podcasts_favorite.setIndicator(customTabTextView(getSherlockActivity().getString(
                    R.string.PodcastManager_Favorite_Title)));
        }else{
            //set tab titles
            podcasts_all.setIndicator(getSherlockActivity().getString(R.string.PodcastManager_All_Title));
            podcasts_downloaded.setIndicator(getSherlockActivity().getString(
                    R.string.PodcastManager_Downloaded_Title));
            podcasts_favorite.setIndicator(getSherlockActivity().getString(R.string.PodcastManager_Favorite_Title));
        }
    
    
        tabHost.addTab(podcasts_all);
        tabHost.addTab(podcasts_downloaded); 
        tabHost.addTab(podcasts_favorite); 
    
    
        //fill background with selector drawable
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){
            setTabsBackground(tabHost);
        }
    
    }
    
    private View customTabTextView(String text){
        TextView txtTab = new TextView(getActivity());
        txtTab.setText(text.toUpperCase());
        txtTab.setPadding(0, 5, 0, 0);
        txtTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        txtTab.setTextColor(Color.DKGRAY);
        txtTab.setGravity(Gravity.CENTER);
        txtTab.setTypeface(Typeface.MONOSPACE, Typeface.BOLD);
        txtTab.setHeight(34);
    
        return txtTab;
    }
    
    private void setTabsBackground(TabHost tabHost) {
        View v;
        int count = tabHost.getTabWidget().getTabCount();
        for (int i = 0; i < count; i++) {
            v = tabHost.getTabWidget().getChildTabViewAt(i);
            v.setBackgroundResource(R.drawable.tab_indicator_compat_holo);
    
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
            //Fix margins in 2.x, by default there is -2  
            params.setMargins(0, 0, 0, 0);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多