Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
xml version="1.0" encoding="utf-8"
<menu xmlnsapp="http://schemas.android.com/apk/res-auto"
xmlnsandroid="http://schemas.android.com/apk/res/android">
<item androidid="@+id/menu_search"
androidicon="@drawable/action_search"
androidtitle="search"
appshowAsAction="ifRoom|collapseActionView"
appactionViewClass="android.support.v7.widget.SearchView"/>
<item androidid="@+id/search_history"
androidtitle="SearchHistory" />
</menu>
Menuの項目が選択された際の処理を実装する
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search_history:
Intent intent = new Intent(this, SearchHistoryActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
階層的なメニューを表示する。
<item androidid="@+id/file"
androidtitle="file" >
<menu>
<item androidid="@+id/create_new"
androidtitle="create_menu" />
<item androidid="@+id/open"
androidtitle="open" />
</menu>
</item>
ToolBar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
xml version="1.0" encoding="utf-8"
<androidsupportv7widgetToolbar xmlnsandroid="http://schemas.android.com/apk/res/android"
xmlnsapp="http://schemas.android.com/apk/res-auto"
androidlayout_width="match_parent"
androidlayout_height="wrap_content"
androidbackground="?attr/colorPrimary"
androidminHeight="?attr/actionBarSize"
androidfitsSystemWindows="true"
androidid="@+id/toolBar"
apptheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</androidsupportv7widgetToolbar>
Backボタン(homeボタン)を有効にする
Toolbar toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
Backボタンの処理を実装する
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra(FROM_SEARCH_HISTORY, "Java");
startActivity(intent);
default:
break;
}
return super.onOptionsItemSelected(item);
}
TabLayout + ViewPager
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewFragmentPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(searchFragment, "Search");
viewPagerAdapter.addFragments(new TrendFragment(), "Trend");
viewPagerAdapter.addFragments(new SubFragment(), "Sub");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
public class ViewFragmentPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
public ViewFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragments(Fragment fragments, String tabTitles){
this.fragments.add(fragments);
this.tabTitles.add(tabTitles);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
xml version="1.0" encoding="utf-8"
<RelativeLayout xmlnsandroid="http://schemas.android.com/apk/res/android"
xmlnstools="http://schemas.android.com/tools"
xmlnsapp="http://schemas.android.com/apk/res-auto"
androidid="@+id/activity_main"
androidlayout_width="match_parent"
androidlayout_height="match_parent"
toolscontext="com.example.atuski.qiitaqlient.MainActivity">
<androidsupportdesignwidgetAppBarLayout
androidlayout_width="match_parent"
androidid="@+id/appBar"
androidlayout_height="wrap_content"
androidtheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include androidlayout_height="wrap_content"
androidlayout_width="match_parent"
layout="@layout/main_tool_bar_layout" />
<androidsupportdesignwidgetTabLayout
androidlayout_width="match_parent"
androidlayout_height="wrap_content"
androidid="@+id/tabLayout"
apptabMode="fixed"
apptabGravity="fill">
</androidsupportdesignwidgetTabLayout>
</androidsupportdesignwidgetAppBarLayout>
<androidsupportv4viewViewPager
androidlayout_width="match_parent"
androidlayout_below="@+id/appBar"
androidlayout_height="match_parent"
androidid="@+id/viewPager">
</androidsupportv4viewViewPager>
</RelativeLayout>