How to remove the margin between the app icon and the edge of the screen on the ActionBar?

前端 未结 1 844
广开言路
广开言路 2021-01-05 07:31

I have a custom home icon for my app and I want it to align all the way to the left of the actionbar, so that it\'s touching the edge of the screen. Is this possible, and if

1条回答
  •  一生所求
    2021-01-05 08:01

    I finally managed to get this. You need to use a custom actionbar view. It's actually quite easy:

    (This is using ActionbarSherlock, it should work with the stock compat library also...)

    • First in your res/values/themes.xml, set the actionbar display options to "custom":

      showCustom
      
    • Then create a file called res/layout/actionbar_layout.xml and place in it something like:

      
      
      
      

    • Next, in your activity code add the following:

      View mActionCustomView = getSherlockActivity().getLayoutInflater()
          .inflate(R.layout.actionbar_layout, null);
      getSherlockActivity().getSupportActionBar().setCustomView(
              mActionCustomView);
      
      ImageView homeIcon = (ImageView)mActionCustomView
          .findViewById(R.id.home_icon);
      int abHeight = getSherlockActivity().getSupportActionBar()
          .getHeight();
      homeIcon.setLayoutParams(new RelativeLayout.LayoutParams(
                  abHeight, abHeight));
      

    That's basically it! Let me know if there's anything I left out. There are some nice benefits of having a customisable actionbar view, just stick to the basics and it will look great.

    enter image description here

    0 讨论(0)
提交回复
热议问题