9

爲了容易片段之間切換,我嵌入Horizo​​ntalScrollView到我的標籤佈局像這樣:Horizo​​ntalScrollView在TabHost在端添加額外的空間

<?xml version="1.0" encoding="utf-8"?> 
<TabHost 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:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <HorizontalScrollView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:fillViewport="true" 
      android:scrollbars="none" > 

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

但在我的碼加法片段後(如下所示)有突然顯示出來的Horizo​​ntalScrollView結束一些額外的空白:

之前滾動

Before scrolling

滾動

After scrolling

後的代碼是非常複雜,但我會盡力表現出的重要組成部分。

{ 
    mTabHost = (TabHost) childLayout.findViewById(android.R.id.tabhost); 
    mTabHost.setup(); 
    FrameLayout tabsFL = (FrameLayout) childLayout.findViewById(android.R.id.tabcontent); 
    tabsFL.setId(TABS_FRAME_ID); 

    for (int i = 0; i < list.size(); i++) { 
     mTabHost.addTab(newTab(String.valueOf(i), list.get(i).getTitle(), tabsFL.getId())); 
    } 

    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String tabId) { 
     updateTab(tabId, Integer.parseInt(tabId), list); 
     } 
    }); 

    //manually load first fragment 
    mTabHost.setCurrentTab(mCurrentTab); 
    updateTab(String.valueOf(mCurrentTab), mCurrentTab, list); 
} 


private TabSpec newTab(String tag, String tabLabel, int tabContentId) { 
    int count = Integer.parseInt(tag); 
    count +=1; 

    View indicator = inflater.inflate(R.layout.details_tab, 
     (ViewGroup) childLayout.findViewById(android.R.id.tabs), false); 
    ((TextView) indicator.findViewById(R.id.text)).setText(count + ". " + tabLabel); 

    TabSpec tabSpec = mTabHost.newTabSpec(tag); 
    tabSpec.setIndicator(indicator); 
    tabSpec.setContent(tabContentId); 
    return tabSpec; 
} 

private void updateTab(String tabId, int id, ArrayList<CustomObject> frags) { 
    mCurrentTab = id; 

    FragmentManager fm = activity.getSupportFragmentManager(); 
    fm.beginTransaction() 
     .replace(TABS_FRAME_ID, DetailsFragment.newInstance(frags.get(id)), tabId) 
     .commitAllowingStateLoss(); 
} 

也無關,但我也有一個問題,即第一個標籤不手動加載(點擊標籤加載片段完美,只是最前面的一個不加載由於某種原因)。

+0

這可能是因爲'的android:fillViewport = 「真」'的'Horizo​​ntalScrollView',嘗試刪除它的一個實例,並看看這是否有效。 'Horizo​​ntalScrollView'和'TabWidget'都具有'android:layout_height =「wrap_content」',所以'fillViewport'可能是原因。 – g00dy

+0

不幸的是,我確實嘗試過,並沒有什麼區別。我想也許我不小心添加了一個額外的(隱形?)選項卡,但無法找到日誌語句可能發生的情況。 –

+0

也許它在'tabsFL'裏面?我不知道,但也許最後一個標籤是半隱形的,chech那一個:) – g00dy

回答

0

我想你是手動添加tabhost如..

@SuppressLint("ResourceAsColor") 
public class MainActivity extends FragmentActivity implements 
    OnTabChangeListener, OnPageChangeListener { 

MyPageAdapter pageAdapter; 
private ViewPager mViewPager; 
private TabHost mTabHost; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mViewPager = (ViewPager) findViewById(R.id.viewpager); 

    // Tab Initialization 
    initialiseTabHost(); 

    // Fragments and ViewPager Initialization 
    List<Fragment> fragments = getFragments(); 
    pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments); 
    mViewPager.setAdapter(pageAdapter); 
    mViewPager.setOnPageChangeListener(MainActivity.this); 
} 

// Method to add a TabHost 
private static void AddTab(MainActivity activity, TabHost tabHost, 
     TabHost.TabSpec tabSpec) { 
    tabSpec.setContent(new MyTabFactory(activity)); 
    tabHost.addTab(tabSpec); 
} 

// Manages the Tab changes, synchronizing it with Pages 
public void onTabChanged(String tag) { 
    int pos = this.mTabHost.getCurrentTab(); 
    this.mViewPager.setCurrentItem(pos); 
    // mTabHost.getTabWidget().setDividerDrawable(null); 
    setSelectedTabColor(); 
} 

@Override 
public void onPageScrollStateChanged(int arg0) { 
} 

// Manages the Page changes, synchronizing it with Tabs 
@Override 
public void onPageScrolled(int arg0, float arg1, int arg2) { 
    int pos = this.mViewPager.getCurrentItem(); 
    this.mTabHost.setCurrentTab(pos); 
    // mTabHost.getTabWidget().setDividerDrawable(null); 
} 

@Override 
public void onPageSelected(int arg0) { 
} 

@SuppressLint("ResourceAsColor") 
private void setSelectedTabColor() { 
    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { 
     mTabHost.getTabWidget().setDividerDrawable(
       android.R.color.transparent); 
     TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i) 
       .findViewById(android.R.id.title); 
     tv.setTextColor(getResources().getColor(R.color.white)); 
     mTabHost.getTabWidget() 
       .setShowDividers(TabWidget.SHOW_DIVIDER_NONE); 
     mTabHost.getTabWidget().getChildAt(i) 
       .setBackgroundColor(R.drawable.bottom_home_back); 
     // mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 
     // 50; 
    } 
    mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()) 
      .setBackgroundResource(R.drawable.btn_selected); 
    // mTabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50; 
} 

private List<Fragment> getFragments() { 
    List<Fragment> fList = new ArrayList<Fragment>(); 

    // TODO Put here your Fragments 
    // DealTab f1 = DealTab.newInstance(); 
    DealTab_New f1 = DealTab_New.newInstance(); 
    EventTab f2 = EventTab.newInstance(); 

    MyAccountFragment f3 = MyAccountFragment.newInstance(); 
    MessageFragment f4 = MessageFragment.newInstance(); 
    MoreFragment f5 = MoreFragment.newInstance(); 
    QrCodeFragment f6 = QrCodeFragment.newInstance(); 

    // fList.add(f1); 
    fList.add(f1); 
    fList.add(f2); 
    fList.add(f3); 
    fList.add(f4); 
    fList.add(f5); 
    fList.add(f6); 

    return fList; 
} 

// Tabs Creation 
@SuppressLint("ResourceAsColor") 
private void initialiseTabHost() { 
    mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(); 

    // TODO Put here your Tabs 
    MainActivity.AddTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab1").setIndicator(
        "", 
        getApplicationContext().getResources().getDrawable(
          R.drawable.btn_deals))); 


    MainActivity.AddTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab2").setIndicator(
        "", 
        getApplicationContext().getResources().getDrawable(
          R.drawable.btn_event))); 
    MainActivity.AddTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab3").setIndicator(
        "", 
        getApplicationContext().getResources().getDrawable(
          R.drawable.btn_my_account))); 
    MainActivity.AddTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab4").setIndicator(
        "", 
        getApplicationContext().getResources().getDrawable(
          R.drawable.btn_message))); 
    MainActivity.AddTab(
      this, 
      this.mTabHost, 
      this.mTabHost.newTabSpec("Tab5").setIndicator(
        "", 
        getApplicationContext().getResources().getDrawable(
          R.drawable.btn_more))); 

    mTabHost.setOnTabChangedListener(this); 

    setSelectedTabColor(); 
} 
} 
在此代碼

我添加上手動及其片段負荷標籤按鈕。

0

TabWidget默認將measureWithLargestChild設置爲true,這意味着它使用最寬的選項卡來計算TabWidget在測量自身時使用的內部LinearLayout的總寬度。如果您的選項卡的長度不同,則會導致內部LinearLayout在較短的選項卡和最寬選項卡之間的差異結尾處具有一些額外的空間。如果您將android:measureWithLargestChild="false"添加到TabWidget佈局,它應該可以解決您的問題。

TL;博士改變你的佈局TabWidget到:

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