Skip to content

Instantly share code, notes, and snippets.

@tmf16
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmf16/9084645 to your computer and use it in GitHub Desktop.
Save tmf16/9084645 to your computer and use it in GitHub Desktop.
ActionBar(android.support.v7.app.ActionBar)に独自Viewを追加する方法
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/menu_hoge"
android:title="hogehoge"
app:showAsAction="always" />
<item
android:id="@+id/menu_delete"
android:title="delete"
android:icon="@android:drawable/ic_delete"
app:showAsAction="always" />
</menu>
package com.example.android;
import com.example.android.R;
import android.content.Context;
import android.support.v4.view.ActionProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class HogeActionProvider extends ActionProvider {
private Context mContext;
public HogeActionProvider(Context context) {
super(context);
mContext = context;
}
@Override
public View onCreateActionView() {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.menu_item_hoge, null);
TextView textView = (TextView)view.findViewById(R.id.item_hoge);
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((TextView)v).setText("fuga");
}
});
return view;
}
}
package com.example.android;
import com.example.android.R;
import com.example.android.HogeActionProvider;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AbstractActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.act_menu, menu);
MenuItemCompat.setActionProvider(menu.findItem(R.id.menu_hoge), new HogeActionProvider(this));
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.menu_delete:
android.util.Log.d("debug", "delete");
break;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/item_hoge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hoge" />
</LinearLayout>
@tmf16
Copy link
Author

tmf16 commented Feb 19, 2014

src/com/example/android/MainActivty.java
src/com/example/android/HogeActionProvider.java

res/layout/home_activity.xml
res/layout/menu_item_hoge.xml
res/menu/act_menu.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment