2014-03-02 33 views
5

一些問題和困難我已經實現了一個可滾動的標籤+刷卡,一切工作正常。 但是我有與當前PagerTabStrip一些問題:PagerTabStrip

我不能讓當前標題大膽

2.不能選擇默認選項卡我要顯示(如:與位置2)在呈現視圖時,像Google Play應用中的選項卡一樣

3.第一個和最後一個選項卡在左側和右側分別有一個邊距,我可以將其設置爲0dp ?

這是我能爲造型的標籤做:

的Java:

strip = (PagerTabStrip) findViewById(R.id.pager_tab_strip); 
strip.setTabIndicatorColorResource(R.color.light_blue); 
strip.setDrawFullUnderline(true); 

佈局:

<android.support.v4.view.PagerTabStrip 
    android:id="@+id/pager_tab_strip" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top" 
    android:background="@drawable/pagertitle_border_bottom" 
    android:paddingBottom="15dp" 
    android:paddingTop="15dp" 
    android:textColor="@color/light_blue" /> 

誰能幫助我的3分?謝謝。

+0

沒有ü找到一個解決方案? –

+0

@ CodePond.org:其實不,使用這個小部件。我發現的唯一解決方案是創建自己的傳呼機:標題可滾動視圖和滑動內容視圖監聽器... – Copernic

+0

請考慮我提出的答案。 –

回答

12

那麼,我真的找到了一個適當的解決方案。忘記PagerTabStrip;)

顯然,Google發佈了一個sample app,它具有他們在Google Play應用中使用的確切源代碼。這是非常容易使用。下載示例應用程序並從那裏複製兩個文件:SlidingTabLayout.javaSlidingTabStrip.java

然後在你的佈局XML,添加上述ViewPager以下(變化com.example你的包名):

<com.example.SlidingTabLayout 
     android:id="@+id/sliding_tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

<android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_marginTop="5dp" 
     android:layout_weight="1.5" 
     android:layout_width="match_parent" 
     android:background="@android:color/white" 
     android:layout_height="0dp"/> 

,並添加以下的活動,您初始化ViewPager後:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(new GridPagerAdapter(getSupportFragmentManager())); 
     // Initialize the SlidingTabLayout. Note that the order is important. First init ViewPager and Adapter and only then init SlidingTabLayout 
     mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs); 
     mSlidingTabLayout.setViewPager(mViewPager); 
    } 

SlidingTabLayout的API允許您輕鬆地進行基本造型,例如更改標籤顏色,分隔線顏色和指示顏色等。

還有一件重要的事情,Google提供的SlidingTabLayout在您有很多標籤時效果很好。但是,例如,如果您有三個選項卡,則會看到所有選項卡均向左對齊,並且最後一個選項卡和視圖寬度之間存在很大差距。 enter image description here

爲了克服這個問題,剛好低於TextView textView = new TextView(context);線173之後修改SlidingTabLayout.java添加以下行:

textView.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));

現在標籤間的空間將被正確地劃分: enter image description here

+1

我愛你______ –

+0

要平均填充視圖,你可以調用它的函數setDistributeEvenly(true); – WISHY

相關問題