ActionBar 和 ViewPager
ActionBar and ViewPager
我正在尝试使用选项卡实现一个简单的应用程序。我正在使用 ViewPager
和 TabLayout
。问题是我不明白为什么没有ActionBar
。这是我的代码:
主要活动:
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);
}}
片段:
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;
} }
activity_main.xml:
<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>
我也在使用 AppCompat.Light.DarkActionBar
主题。提前致谢
Also i'm using AppCompat.Light.DarkActionBar theme
与此冲突:
public class MainActivity extends FragmentActivity
如果您想使用 appcompat-v7
,您必须始终如一地使用它,包括从 AppCompatActivity
扩展(它本身扩展 FragmentActivity
)。
尝试从 AppCompatActivity
扩展您的 MainActivity
使用:
public class MainActivity extends AppCompatActivity
而不是:
public class MainActivity extends FragmentActivity
或者, 您可以将 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>
我正在尝试使用选项卡实现一个简单的应用程序。我正在使用 ViewPager
和 TabLayout
。问题是我不明白为什么没有ActionBar
。这是我的代码:
主要活动:
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);
}}
片段:
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;
} }
activity_main.xml:
<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>
我也在使用 AppCompat.Light.DarkActionBar
主题。提前致谢
Also i'm using AppCompat.Light.DarkActionBar theme
与此冲突:
public class MainActivity extends FragmentActivity
如果您想使用 appcompat-v7
,您必须始终如一地使用它,包括从 AppCompatActivity
扩展(它本身扩展 FragmentActivity
)。
尝试从 AppCompatActivity
MainActivity
使用:
public class MainActivity extends AppCompatActivity
而不是:
public class MainActivity extends FragmentActivity
或者, 您可以将 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>