操作栏未显示PagerSlidingTabStrip

时间:2015-08-05 17:32:49

标签: android android-actionbar pagerslidingtabstrip

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

enter image description here

有没有人帮我解决为什么操作栏被禁用。

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>

预期是:

enter image description here

2 个答案:

答案 0 :(得分:1)

可能只是在你的xml文件中,你创建了Actionbar 0dp。另一个问题可能是您没有为App使用正确的主题。获取操作栏的主题将在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支持的材料设计指南http://www.android4devs.com/2014/12/how-to-make-material-design-app.html

答案 1 :(得分:1)

我遇到了同样的问题,要解决它只是扩展ActionBarActivity而不是FragmentActivity,它会起作用。