2016-07-18 6 views
1

スワイプタブビューを正常に動作していますが、タブビューの前に表示されていたときにアクティビティのアクションバーが消えてしまう問題があります。タブビューの上部にアクションバーを再度表示する方法を教えてください。スワイプタブビューでアクションバーが表示されなくなった

MainActivity:

public class MainActivity extends FragmentActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Get the view from activity_main.xml 
     setContentView(R.layout.activity_main); 

Toolbar myToolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(myToolbar); 

     // Locate the viewpager in activity_main.xml 
     ViewPager viewPager = (ViewPager) findViewById(R.id.pager); 

     ((ViewPager.LayoutParams) (findViewById(R.id.tab_strip)).getLayoutParams()).isDecor = true; 

     // Set the ViewPagerAdapter into ViewPager 
     viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager())); 
    } 

} 

Tab1を:

public class ViewPagerAdapter extends FragmentPagerAdapter { 

    final int PAGE_COUNT = 3; 
    // Tab Titles 
    private String tabtitles[] = new String[] { "Tab1", "Tab2", "Tab3" }; 
    Context context; 

    public ViewPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

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

    @Override 
    public Fragment getItem(int position) { 
     switch (position) { 

      // Open FragmentTab1.java 
     case 0: 
      FragmentTab1 fragmenttab1 = new FragmentTab1(); 
      return fragmenttab1; 

      // Open FragmentTab2.java 
     case 1: 
      FragmentTab2 fragmenttab2 = new FragmentTab2(); 
      return fragmenttab2; 

      // Open FragmentTab3.java 
     case 2: 
      FragmentTab3 fragmenttab3 = new FragmentTab3(); 
      return fragmenttab3; 
     } 
     return null; 
    } 

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

のstyles.xml:

simlar TAB2及びTAB3ため パブリッククラスFragmentTab1フラグメント{

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // Get the view from fragmenttab1.xml 
     View view = inflater.inflate(R.layout.fragmenttab1, container, false); 
     return view; 
    } 

} 

ViewPageAdapterが延びます

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="windowActionBar">true</item> 
     <item name="windowNoTitle">false</item> 
    </style> 

マニフェスト:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.zohaibsiddique.myapplication"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity android:name=".MainActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/tool_bar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:elevation="10dp" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     android:minHeight="?attr/actionBarSize" /> 

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

    <android.support.v4.view.PagerTabStrip 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/tab_strip" 
     android:layout_gravity="top" 
     android:paddingBottom="10dp" 
     android:paddingTop="10dp" 
     android:textColor="#000000" /> 

</android.support.v4.view.ViewPager> 
    </LinearLayout> 
+0

マニフェストファイルをここに置くことはできますか? –

+0

私はそれを入れました。今すぐご覧ください –

+0

'FragmentActivity'を' ActionBarActivity'や 'AppCompatActivity'に変更しようとしています。 – Lawrance

答えて

0

は、ページのタイトルを表示するPagerTabStripを追加します。詳細については

<android.support.v4.view.ViewPager 
    android:id="@+id/vpPager" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.v4.view.PagerTabStrip 
     android:id="@+id/pager_header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="top" 
     android:paddingBottom="4dp" 
     android:paddingTop="4dp" /> 

</android.support.v4.view.ViewPager> 

を、このリンクを参照してください。

https://github.com/codepath/android_guides/wiki/ViewPager-with-FragmentPagerAdapter

+0

それは動作していません –

+0

私のコードを編集したstyles.xmlファイルを見てください。 –

+0

私の編集した答えを見てください。 –

0

私は自分で自分の問題を解決します。 MainActivityをAppCompatActivityに拡張し、アクションバーを再表示します。

関連する問題