【问题标题】:How to remove the margin between the app icon and the edge of the screen on the ActionBar?如何去除应用程序图标和ActionBar上屏幕边缘之间的边距?
【发布时间】:2012-03-05 04:10:36
【问题描述】:

我的应用有一个自定义主页图标,我希望它一直对齐到操作栏的左侧,以便它接触到屏幕的边缘。这可能吗?如果可以,怎么做?我没有看到任何设置填充或边距以使其一直向左对齐的东西。

【问题讨论】:

  • 主页图标是否有填充或边距?如果是这样,请删除它们。顺便说一句,向我们展示一些来自 xml 文件的代码,然后这里的许多代码可以帮助您。
  • 它没有xml。刚刚将 android:icon="@drawable/my_icon" 添加到清单和 ab.setDisplayShowHomeEnabled(true); ab.setDisplayUseLogoEnabled(false);到活动

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


【解决方案1】:

我终于设法得到了这个。您需要使用自定义操作栏视图。其实很简单:

(这是使用 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. -->
    

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

    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)。
  • @OnuraySahin 您可以在代码developer.android.com/reference/android/app/…中设置相同的选项
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 2017-05-28
  • 1970-01-01
  • 2015-11-05
  • 1970-01-01
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多