在android中的操作栏中隐藏图标

时间:2012-10-16 07:51:49

标签: android android-layout

我有一个以程序化方式创建的操作栏:

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来做到这一点。我想知道如何以程序化的方式做到这一点?有什么想法吗?

0 个答案:

没有答案