プログラムで作成されたアクションバーがあります:
public class ActionBarPdfView extends Activity{
public ImageView action_bar_home_icon, action_bar_back_icon, action_bar_star, action_bar_search;
public RelativeLayout relative;
public ActionBar action;
public RelativeLayout.LayoutParams params;
private DatabaseManager dbManager;
public static int VIEW_ID = 3;
public int getViewId(){
return VIEW_ID;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
action_bar_home_icon = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
action_bar_home_icon.setLayoutParams(params);
action_bar_home_icon.setId(1);
action_bar_star = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
params.setMargins(0, 0, 20, 0);
action_bar_star.setLayoutParams(params);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
action = getActionBar();
relative = new RelativeLayout(getApplicationContext());
action_bar_back_icon = new ImageView(getApplicationContext());
action_bar_search = new ImageView(getApplicationContext());
dbManager = new DatabaseManager(getApplicationContext());
dbManager.waitForTurn();
ObjectsDBAdapter objects = dbManager.getObjectsDbAdapter();
objects.setupImageView(R.drawable.home_android, action_bar_home_icon);
// action_bar_home_icon.setImageResource(R.drawable.home_android);
// params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
// params.setMargins(0, 0, 20, 0);
// action_bar_back_icon.setImageResource(R.drawable.back_android);
// action_bar_back_icon.setLayoutParams(params);
// action_bar_back_icon.setId(2);
objects.setupImageView(R.drawable.sternbutton_android, action_bar_star);
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0, 0, 20, 0);
objects.setupImageView(R.drawable.searchtext_android, action_bar_search);
action_bar_search.setLayoutParams(params);
action_bar_search.setId(3);
action.setDisplayShowCustomEnabled(true);
action.setDisplayShowHomeEnabled(false);
action.setDisplayShowTitleEnabled(false);
relative.addView(action_bar_home_icon);
relative.addView(action_bar_back_icon);
relative.addView(action_bar_star);
relative.addView(action_bar_search);
relative.setGravity(Gravity.CENTER);
action.setCustomView(relative);
dbManager.closeObjectsDbAdapter();
return true;
}
}
左側にいくつかのアイコンがあり、右側にいくつかのアイコンがあります。私が欲しいのは、右からのアイコンが多く、左側のいくつかのアイコンを覆っているときに、このアイコンをメニューで非表示にすることです。
xml では、android:showAsAction を使用してそれを実行できることを知っています。プログラムでそれを行う方法を知りたいですか?何か案は?