Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active December 19, 2015 23:59
Show Gist options
  • Save daichan4649/6037888 to your computer and use it in GitHub Desktop.
Save daichan4649/6037888 to your computer and use it in GitHub Desktop.
custom actionbar (for Android) MenuFragment 動的切替
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/title"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="Title"
android:textColor="#FFFFFF" />
</LinearLayout>
<activity
android:name=".XxxActivity"
android:theme="@style/Custom" />
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_edit"
android:showAsAction="always"
android:title="編集"/>
</menu>
public class MenuFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
break;
}
return super.onOptionsItemSelected(item);
}
}
<style name="Custom" parent="android:Theme.Holo.Light">
<!-- actionbar全体 -->
<item name="android:actionBarStyle">@style/Custom.ActionBarStyle</item>
<!-- actionItem -->
<item name="android:actionMenuTextColor">#FFFFFF</item>
</style>
<style name="Custom.ActionBarStyle" parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@drawable/color_title</item>
<item name="android:titleTextStyle">@style/Custom.ActionBarStyle.TitleTextStyle</item>
</style>
<style name="Custom.ActionBarStyle.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#FFFFFF</item>
</style>
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xxx);
// actionbar
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getActionBar().setCustomView(R.layout.actionbar);
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
// menu動的表示(show/hide で切替)
MenuFragment menuFragment = null;
if (getFragmentManager().findFragmentByTag("menu") == null) {
menuFragment = new MenuFragment();
}
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(menuFragment, "menu");
ft.commit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment