【问题标题】:How to align items in action bar to the left?如何将操作栏中的项目向左对齐?
【发布时间】:2014-09-22 11:27:29
【问题描述】:

我有一个带有搜索视图的操作栏,但我想将其对齐到左侧,例如在地图应用程序中而不是右侧。我该怎么做?

【问题讨论】:

    标签: android android-3.0-honeycomb android-actionbar


    【解决方案1】:

    你可以这样做 试试这个肯定会有帮助:

    ActionBar action=getActionBar();
    action.setDisplayShowCustomEnabled(true);
    action.setDisplayShowHomeEnabled(false);
    action.setDisplayShowTitleEnabled(false);
    RelativeLayout relative=new RelativeLayout(getApplicationContext());
    relative.addView(new SearchView(getApplicationContext()));
    action.setCustomView(relative);
    

    不要忘记将布局参数添加到您的搜索视图,以便它可以右对齐到相对。 如果有任何问题,请告诉我。 谢谢你

    然后它将显示在操作栏的右侧。 还有

    【讨论】:

      【解决方案2】:

      不要使用 getApplicationContext() ,我发现了一个问题

          RelativeLayout relative = new RelativeLayout(mActionBar.getThemedContext());
          mSearchView = new SearchView(mActionBar.getThemedContext());
          mSearchView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
          mSearchView.setIconifiedByDefault(false);
          mSearchView.setGravity(Gravity.LEFT);
          relative.addView(mSearchView);
          mActionBar.setDisplayShowCustomEnabled(true);
          mActionBar.setCustomView(relative);
      

      【讨论】: