2014-12-19 134 views
-1

我創造我的第一個應用程序,並學習首次Java作爲井的過程中,和我停留在工具欄上創建標籤..將標籤添加到Android 5.0工具欄?

我看了看this崗位。不過,我仍然對如何正確實施它們感到困惑。我是否應該下載SlidingTabsColors文件,然後將它作爲gradle.build中的依賴項添加?

在本節:fragment_sample.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <android.support.v7.widget.Toolbar 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/my_awesome_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 

      app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <com.example.android.common.view.SlidingTabLayout 
        android:id="@+id/sliding_tabs" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
      </LinearLayout> 
     </android.support.v7.widget.Toolbar> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="0px" 
      android:layout_weight="1" 
      android:background="@android:color/white" /> 

    </LinearLayout> 

我不能添加com.example.android.common.view.SlidingTabLayout佈局。

謝謝

回答

1

試試這個

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <android.support.v7.widget.Toolbar 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/my_awesome_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize" 
      app:theme="@style/ThemeOverlay.AppCompat.ActionBar"> 
     </android.support.v7.widget.Toolbar> 

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

     <android.support.v4.view.ViewPager 
      android:id="@+id/viewpager" 
      android:layout_width="match_parent" 
      android:layout_height="0px" 
      android:layout_weight="1" 
      android:background="@android:color/white" /> 

    </LinearLayout> 

爲了更好地實現嘗試:

添加到您的gradle這個依賴性

compile 'com.astuetz:pagerslidingtabstrip:1.0.1' 

然後代替SlidingTabLayout使用

<com.astuetz.PagerSlidingTabStrip 
    android:id="@+id/tabs" 
    android:layout_width="match_parent" 
    android:layout_height="48dip" /> 

在您的活動要在ViewPager你打電話

// Initialize the ViewPager and set an adapter 
ViewPager pager = (ViewPager) findViewById(R.id.viewpager); 
pager.setAdapter(new YourCustomAdapter(getSupportFragmentManager())); 

// Bind the tabs to the ViewPager 
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs); 
tabs.setViewPager(pager); 

你可能想實現這樣的適配器:

public class YourCustomAdapter extends FragmentStatePagerAdapter { 
    private String[] titles = { "Item 1", "Item 2", "Item 3" }; 
    public YourCustomAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) { 
     switch(position){ 
      case 0:{ 
       return new YourFragment(); 
      } 
      case 1:{ 
       return new YourFragment(); 
      } 
      case 2:{ 
       return new YourFragment(); 
      } 
     } 
     return null; 
    } 

    @Override 
    public int getCount() { 
     return titles.length; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     return titles[position]; 
    } 
} 

希望它可以幫助!

+0

謝謝,生病了試試看! – 2014-12-25 00:21:16

相關問題