2016-03-28 49 views
1

我目前正在將標籤添加到我的活動中。同樣,我使用Google SlidingTabLayoutSlidingTabStrip片段中的Textview不起作用

我的片段XML看起來是這樣的:

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

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Large Text" 
     android:id="@+id/textViewTab" /> 


</RelativeLayout> 

我的內容的XML看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.musicisfun.allaboutfootball.MainActivity" 
    tools:showIn="@layout/activity_main"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout 
     android:layout_width="match_parent" 
     android:id="@+id/tabs" 
     android:layout_height="wrap_content"> 
    </com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout> 

    <android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     /> 

</LinearLayout> 

</RelativeLayout> 

和我的代碼如下所示:

public static class TabFragment extends Fragment{ 

     public static TabFragment getInstance(int position){ 
      TabFragment tabFragment = new TabFragment(); 
      Bundle bundle = new Bundle(); 
      bundle.putInt("site",position); 
      tabFragment.setArguments(bundle); 
      return tabFragment; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View layout = inflater.inflate(R.layout.fragment_tab,container,false); 
      TextView textViewTab = (TextView) layout.findViewById(R.id.textViewTab); 
      Bundle bundle = getArguments(); 
      if (bundle!=null){ 
       textViewTab.setText("Current Tab is" + bundle.getInt("site")); 

      } 
      return layout; 
     } 
    } 

我的片段適配器長相像這樣:

class TabPagerAdaptor extends FragmentPagerAdapter{ 

     String[] tab_names; 
     public TabPagerAdaptor(FragmentManager fm) { 
      super(fm); 
      tab_names=getResources().getStringArray(R.array.feed_names); 
     } 

     @Override 
     public Fragment getItem(int position) { 
      TabFragment tabFragment = TabFragment.getInstance(position); 
      return tabFragment; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 

      return tab_names[position]; 
     } 

     @Override 
     public int getCount() { 
      return 2; 
     } 


    } 

,這就是我如何調用標籤:

mViewPager= (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager())); 
    mTabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    mTabs.setViewPager(mViewPager); 

但是當我運行的代碼,我無法找到正在顯示的TextView即使兩個選項卡工作的罰款。

+0

你可以發佈你在哪裏設置ViewPager適配器的代碼? –

+0

我已更新代碼 – nathandrake

回答

0

無法查看你的TextView因爲ViewPager的高度是在XML 0dp -

<android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     /> 

,你必須改變要麼wrap_contentmatch_parent -

<android.support.v4.view.ViewPager 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/pager" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 

    /> 

而且,你的LinearLayout的方向是horizontal,寬度SlidingTabLayoutmatch_parentSlidingTabLayout佔據整個屏幕寬度,沒有空間顯示Viewpager

你的LinearLayout的方向應垂直不是水平

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout 
     android:layout_width="match_parent" 
     android:id="@+id/tabs" 
     android:layout_height="wrap_content"> 
    </com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout> 

    <android.support.v4.view.ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     /> 

</LinearLayout> 
+0

我對match_parent進行了更改,結果仍然相同。 – nathandrake

+0

編輯了答案。 –

+0

我把它做成水平的,因爲當我試圖將它設置爲垂直時,工作室給出了一個錯誤,但現在它的工作很棒..切合你的支持 – nathandrake