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

人走茶凉 提交于 2019-11-30 19:47:13

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":

    <item name="android:displayOptions">showCustom</item>
    
  • Then create a file called res/layout/actionbar_layout.xml and place in it something like:

    <ImageView
        android:id="@+id/home_icon"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:scaleType="centerCrop"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />
    
    <!-- Add textviews or whatever you like here to replicate the actionbar
         title etc. -->
    

  • 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!