android - 如何去除 ActionBar 上应用程序图标和屏幕边缘之间的边距?

标签 android android-3.0-honeycomb android-actionbar

我的应用程序有一个自定义主页图标,我希望它一直对齐到操作栏的左侧,以便它接触到屏幕的边缘。这是否可能,如果可能,如何实现?我没有看到任何设置填充或边距以使其一直向左对齐的内容。

最佳答案

我终于成功了。您需要使用自定义操作栏 View 。其实很简单:

(这是使用 ActionbarSherlock,它应该也可以与标准兼容库一起使用...)

  • 首先在您的 res/values/themes.xml 中,将操作栏显示选项设置为“自定义”:

    <item name="android:displayOptions">showCustom</item>
    
  • 然后创建一个名为 res/layout/actionbar_layout.xml 的文件并在其中放置如下内容:

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

  • 接下来,在您的 Activity 代码中添加以下内容:

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

基本上就是这样!如果我遗漏了什么,请告诉我。 拥有可自定义的操作栏 View 有一些不错的好处,只要坚持基本原则,它就会看起来很棒。

enter image description here

关于android - 如何去除 ActionBar 上应用程序图标和屏幕边缘之间的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9265927/

相关文章:

android - 如何获取SeekBar progress drawable的高度

android - 如何在 Android 3.0 (Honeycomb) 中调暗系统栏?

android - Appwidget 大小计算

android - 如何实现类似 Google 搜索中的 ActionBar 滚动动画?

android - facebook android允许权限窗口问题

android - 如何从纬度和经度获取当前位置的天气?

android - Android 上的 NavigationDrawer(汉堡菜单)图标图像

android - 为什么 Cast MediaRouteButton 即使在 Holo.Light Action-Bar 上也总是白色的?

android - 如何在底部设置选项卡并隐藏顶部操作栏?

java - 如何在android honeycomb中编辑EditTextPreference?