0

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

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

4

0 回答 0