2016-03-10 48 views
2

我試圖創建一個標籤使用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> 
+0

很樂意幫助你快樂編碼:) –

+0

謝謝:)這意味着很多 – user1241241

回答

0

嘗試這種方式來定製您的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

enter image description here

0

現在,你不應該使用TabHost創建刷卡頁面與Tabs.The更好的辦法才達到它是:

對於標籤做以下滑動頁面。

在github上下載或複製以下兩個文件並粘貼您的項目。除了setDistributeEvenly方法之外,這與developers.google.com上的相同。

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); 
+0

是的,邁克是正確的。有人可以編輯這個答案。我會改變它 –

+0

對不起,我在這裏是新的。我不明白雅贊說什麼?雅贊可以換句話來說,還是更具說明性? –