android - 自定义操作栏选项卡 View

标签 android android-actionbar fragment tabview

我正在尝试在操作栏中创建一个自定义选项卡 View ,如下所示: enter image description here

我得到了这个观点:

enter image description here

我想更改选项卡 View 指示器的背景,因此背景颜色变得不可见,并且我们在选定的选项卡 View 指示器下有绿线。

这是我的代码(我使用默认选项卡 View ):

public class BaseActivity extends Activity{

    ActionBar bar;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState); setContentView(R.layout.base_activity);

        bar = getActionBar(); 

        // Change action bar background

        BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.background_actionbar)); background.setTileModeX(android.graphics.Shader.TileMode.REPEAT); 

        bar.setBackgroundDrawable(background);

        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

        ActionBar.Tab tabA = bar.newTab().setText(getResources().getString(R.string.documents)); 

        ActionBar.Tab tabB = bar.newTab().setText(getResources().getString(R.string.videos)); 

        ActionBar.Tab tabC = bar.newTab().setText(getResources().getString(R.string.menu_settings)); 

        Fragment fragmentA = new DocumentsListFragment();

        Fragment fragmentB = new VideosListFragment();

        Fragment fragmentC = new UserAccountFragment();

        tabA.setTabListener(new MyTabsListener(fragmentA)); 

        tabB.setTabListener(new MyTabsListener(fragmentB)); 

        tabC.setTabListener(new MyTabsListener(fragmentC)); 

        bar.addTab(tabA);

        bar.addTab(tabB);

        bar.addTab(tabC);
    }


    protected class MyTabsListener implements ActionBar.TabListener
    {
        private Fragment fragment;

        public MyTabsListener(Fragment fragment) {
            this.fragment = fragment; }

        @Override

        public void onTabSelected(Tab tab, FragmentTransaction ft)
        {
            ft.replace(R.id.fragment_place, fragment, null); }

        @Override

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

        }

        @Override

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

        }
    }

}

请问,我怎样才能膨胀这些标签 View 指示器。

最佳答案

将这些添加到 styles.xml 中的值下

 <style name="Theme.AndroidDevelopers" parent="Theme.Sherlock.Light.ForceOverflow"> 
 <item name=" android:actionBarItemBackground">@drawable/ad_selectable_background
 </item>
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
    <item name="actionBarTabStyle">@style/MyActionBarTabStyle</item>
 </style>




<style name="MyActionBarTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar">
    <item name="android:background">@drawable/actionbar_tab_bg</item>
   <item name="android:gravity">center</item>
   <item name="android:layout_gravity">center</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
</style>

这类似于您的选项卡选择器:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ad_tab_selected_pressed_holo"           android:state_selected="true"/>
<item android:drawable="@android:color/transparent"/>

关于android - 自定义操作栏选项卡 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11949838/

相关文章:

android - ActionBar 和 ViewPager 以及 CursorLoader 可以工作,但也许有更好的方法

android - 如何在向下滚动网页 View 时隐藏操作栏?

java - 在 Theme.AppCompat.Light 中添加图标

android - 正在运行的Fragment和Activity之间的通信

Android:微调器在 Activity 中工作,但在 Fragment 中不工作

android - 从安卓手机获取IMSI?

java - Android Studio 无法识别导入语句

android - 循环 ViewPager。 Fragments 在第一轮后无法正常工作

java - Android ScrollView,检查当前是否滚动

android - 检查工作 AsyncTask