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

Android 无法自定义片段选项卡主机的选项卡,android,fragment-tab-host,Android,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:layou

我正在尝试使用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>
欲了解更多信息,请参阅


尝试用这种方式自定义您的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>
欲了解更多信息,请参阅


现在,您不应该使用TabHost创建带有选项卡的滑动页面。实现这一点的更好方法是:

对于带有选项卡的滑动页面,请执行以下操作

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

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"
    />
tab_view.xml(仅查看选项卡,如果需要,也可以在此处使用ImageView)


现在,您不应该使用TabHost创建带有选项卡的滑动页面。实现此功能的更好方法是:

对于带有选项卡的滑动页面,请执行以下操作

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

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"
    />
tab_view.xml(仅查看选项卡,如果需要,也可以在此处使用ImageView)


很高兴帮助你快乐地编码:)谢谢:)这意味着一个LOT很高兴帮助你快乐地编码:)谢谢:)这意味着一个LOT是的,迈克是正确的。有人可以编辑这个答案。我会改的。对不起,我是新来的。我不明白亚赞说了什么?亚赞可以用其他的话或者更具解释性的话来解释吗?是的,迈克是对的。有人可以编辑这个答案。我会改的。对不起,我是新来的。我不明白亚赞说了什么?亚赞可以用其他的话或者更具解释性的话来表达吗?
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];
    } 
 } 
 <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>
 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);