如何在ActionBar上移除应用图标和屏幕边缘之间的边距?

7

我有一个自定义的应用程序主页图标,并希望它能够与actionbar完全靠左对齐,与屏幕边缘相接触。是否有可能实现这一点,如果可以,该如何实现?我没有找到任何设置填充或外边距以使其完全对齐到左侧的选项。


主页图标是否有填充或边距?如果有,请将其删除。顺便展示一下XML文件中的代码,这样就可以得到更多人的帮助了。 - Jay Mayu
没有xml文件。只需将android:icon="@drawable/my_icon"添加到清单中,以及ab.setDisplayShowHomeEnabled(true); ab.setDisplayUseLogoEnabled(false);到活动中。 - CACuzcatlan
1个回答

5

我终于做到了。您需要使用自定义操作栏视图。实际上非常容易:

(这是使用ActionbarSherlock,也应该适用于股票兼容库...)

  • 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));
    

基本上就是这样了!如果有遗漏的地方,请告诉我。

拥有可自定义的操作栏视图有一些不错的好处,只需坚持基本原则,它看起来会很棒。

在此输入图片描述


您不需要设置homeicon布局参数,只需将@+id/home_icon的边距设置为0(而不是示例中的5dp)。 - scottyab
@OnuraySahin 你可以在代码中设置相同的选项 http://developer.android.com/reference/android/app/ActionBar.html#setDisplayShowCustomEnabled%28boolean%29 - brk3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接