无法自定义片段选项卡主机的选项卡

Unable to customize the tabs of fragment tab host

我正在尝试使用 android.support.v4.app.FragmentTabHost.

创建标签

我正在尝试将选项卡的背景颜色更改为白色,将选项卡的文本颜色更改为黑色,但它不起作用。

以下是我的代码。

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    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:textColor="@color/trans_white"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
</android.support.v4.app.FragmentTabHost>

尝试通过这种方式自定义您的 tabhost
tab_selector.xml

<?xml version="1.0" encoding="utf8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<! When selected, use grey >
<item android:drawable="@drawable/tabselectedcolor" android:state_selected="true"/>
<! When not selected, use white >
<item android:drawable="@drawable/tabunselcolor"/>

 </selector>

XML

<?xml version="1.0" encoding="utf8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/host"
    android:layout_width="fillparent"
    android:layout_height="fillparent" >

    <RelativeLayout
        android:layout_width="fillparent"
        android:layout_height="fillparent"
        android:orientation="vertical"
        android:padding="6dp" >

        <TabWidget
            android:id="@android:id/tab"
            android:layout_width="fillparent"
            android:layout_height="wrapcontent" />

        <FrameLayout
            android:layout_below="@android:id/tab"
            android:id="@android:id/tabattributes"
            android:layout_width="fillparent"
            android:layout_height="fillparent"
            android:padding="6dp" />
    </RelativeLayout>

</TabHost>

Java代码

public class TabCustomizationActivity extends TabActivity implements
        OnTabChangeListener {
    /** Called when the activity is first created. */
    TabHost tHost;

    @Override
    public void onCreate(savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources resources = getResources();

        tHost = getTabHost();
        TabHost.TabSpec tSpec;
        Intent intent;

        intent = new Intent().setClass(this, FirstActivity.class);

        tSpec = tHost.newTabSpec("first").setIndicator("One")
                .setContent(intent);
        tHost.addTab(tSpec);

        intent = new Intent().setClass(this, FirstActivity.class);
        tSpec = tHost.newTabSpec("second").setIndicator("Second")
                .setContent(intent);
        tHost.addTab(tSpec);

        intent = new Intent().setClass(this, FirstActivity.class);
        tSpec = tHost.newTabSpec("third").setIndicator("Third")
                .setContent(intent);
        tHost.addTab(tSpec);

        tHost.setCurrentTab(0); // Default Selected Tab

        tHost.setOnTabChangedListener(this);

        tHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
        tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
        tHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
        tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
        tHost.getTabWidget().getChildAt(2).getLayoutParams().height = 40;
        tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);

        tHost.getTabWidget().getChildAt(0)
                .setBackgroundColor(Color.rgb(00, 219, 239));

    }

    @Override
    public void onTab(String tabId) {
        if (tabId.equals("first")) {
            tHost.getTabWidget().getChildAt(0)
                    .setBackgroundResource(R.drawable.tab_selector);
            tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
            tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
        } else if (tabId.equals("second")) {
            tHost.getTabWidget().getChildAt(1)
                    .setBackgroundResource(R.drawable.tab_selector);

            tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
            tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
        } else if (tabId.equals("third")) {
            tHost.getTabWidget().getChildAt(2)
                    .setBackgroundResource(R.drawable.tab_selector);
            tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
            tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
        }

    }

}

有关更多信息,请参阅 this

现在您不应该使用 TabHost 创建滑动页面 Tabs.The 实现它的更好方法是:

对于带标签的滑动页面,请执行以下操作。

在 github 下载或复制以下两个文件并粘贴您的项目。这与 developers.google.com 相同,除了 setDistributeEvenly 方法。

https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabLayout.java

https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/SlidingTabStrip.java

activity_main.xml

  <your.package.name.SlidingTabLayout
    android:clickable="true"
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
     </your.package.name.SlidingTabLayout>

     <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    />

MyAdapter.java(这里我只用了两页)

class MyPagerAdapter extends FragmentPagerAdapter 
{ 
    String[] title = {"All","Favourites"};
    public MyPagerAdapter(FragmentManager fm) { 
        super(fm); 
    } 
    @Override 
    public Fragment getItem(int position) {
        Fragment fragment=null; 
         if (position==0)
            fragment= new All(); 
             if (position==1)
                fragment= new Favourites(); 
        return fragment; 
    } 
    @Override 
     public int getCount() { 
        return 2; 
    } 
       @Override 
       public CharSequence getPageTitle(int position) {
             return title[position];
    } 
 } 

tab_view.xml(仅选项卡视图,如果您需要也可以在此处使用 ImageView)

 <FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <TextView
        android:id="@+id/tab_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:padding="15dp"
        android:textStyle="bold"
        android:textSize="25dp"
        />
   </FrameLayout>

MainActivity.java

 private SlidingTabLayout tabLayout;
 private ViewPager pager;
 tabLayout= (SlidingTabLayout) findViewById(R.id.tabs); 
 pager = (ViewPager) findViewById(R.id.pager); 
 tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title); 
 MyPagerAdapter adapter =  new MyPagerAdapter(getSupportFragmentManager()); 
 pager.setAdapter(adapter); 
 tabLayout.setDistributeEvenly(true); 
 tabLayout.setViewPager(pager);