操作栏不显示与 PagerSlidingTabStrip

Actionbar not showing with PagerSlidingTabStrip

我使用 PagerSlidingTabStrip 创建了一个滑动标签。它创建正确但操作栏丢失它显示如下:

是否可以帮助我解决为什么操作栏被禁用的问题。

import com.astuetz.PagerSlidingTabStrip;


public class SMainTabActivity extends FragmentActivity implements ETabFragment.OnFragmentInteractionListener,
    MTabFragment.OnFragmentInteractionListener{

public String classSelected;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);
    setProgressBarIndeterminateVisibility(false);
    setContentView(R.layout.activity_subject_main_tab);

    Bundle b = getIntent().getExtras();
    classSelected = b.getString("classId");
  // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager()));

    // Give the PagerSlidingTabStrip the ViewPager
    PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    // Attach the view pager to the tab strip
    tabsStrip.setViewPager(viewPager);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actionsbar, menu);

    return super.onCreateOptionsMenu(menu);


   // getMenuInflater().inflate(R.menu.activity_main_actionsbar, menu);
    //return true;
}

xml (activity_subject_main_tab):

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

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    app:pstsShouldExpand="true"
    app:pstsTextAllCaps="true"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:textSize="14sp"
    android:textColor="#000000"
    app:pstsDividerColor="@color/color1"
    app:pstsIndicatorColor="@color/color2"
    app:pstsUnderlineColor="@color/color3"
    app:pstsTabPaddingLeftRight="14dp">
</com.astuetz.PagerSlidingTabStrip>

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

清单:

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SubjectMainTabActivity"
        android:label="@string/title_activity_subject_main_tab" >
    </activity>
</application>

预计是:

可能只是因为在您的 xml 文件中,您将 Actionbar 设置为 0dp。另一个问题可能是您没有为应用程序使用正确的主题。获取操作栏的主题将在您的 style.xml 中设置,如下所示:

<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">true</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

然后在您的 AndroidManifest.xml 中调用它,如下所示:

android:theme="@style/MyMaterialTheme"

注意:您需要将 styles.xml 中的颜色添加到您的选择中。 此外,如果您完全卡住了,请使用此示例以获得更好的 Android 支持 Material 设计指南 http://www.android4devs.com/2014/12/how-to-make-material-design-app.html

我遇到了同样的问题,要解决它,只需扩展 ActionBarActivity 而不是 FragmentActivity 即可。