ActionBar和ViewPager

时间:2017-05-13 15:39:32

标签: android android-viewpager android-actionbar

我正在尝试使用标签实现一个简单的应用。我正在使用TabLayoutActionBar。问题是我无法理解为什么没有public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager)findViewById(R.id.pager); TabLayout tabLayout = (TabLayout)findViewById(R.id.tab_layout); PagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); tabLayout.setupWithViewPager(viewPager); } public class MyViewPagerAdapter extends FragmentStatePagerAdapter { public MyViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return Fragment.newInstance(position); } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { return "Page "+position; } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); }} 。这是我的代码:

enter image description here

MainActivity:

public class Fragment extends android.support.v4.app.Fragment {
int pos;
public Fragment() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(fragment, container, false);
    TextView textView = (TextView)view.findViewById(R.id.text_view);
    textView.setText("Page "+pos);
    return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pos = getArguments().getInt("position");
    setHasOptionsMenu(true);
}
 static Fragment newInstance (int pos) {
    Fragment fragment = new Fragment();
    Bundle args = new Bundle();
    args.putInt("position",pos);
    fragment.setArguments(args);
    return fragment;
} }

片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_below="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

activity_main.xml中:

AppCompat.Light.DarkActionBar

我也使用WebListener主题。提前致谢

2 个答案:

答案 0 :(得分:1)

  

我也使用AppCompat.Light.DarkActionBar主题

与此冲突:

public class MainActivity extends FragmentActivity

如果您想使用appcompat-v7,则必须始终如一地使用它,其中包括从AppCompatActivity扩展(其本身扩展为FragmentActivity)。

答案 1 :(得分:0)

尝试从MainActivity

延长AppCompatActivity

使用:

public class MainActivity extends AppCompatActivity

而不是:

public class MainActivity extends FragmentActivity

OR,您可以将Toolbar添加到您的版面XML,如下所示:

<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
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_below="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>