我的应用程序有一个自定义主页图标,我希望它一直与操作栏的左侧对齐,以便它接触屏幕边缘。这可能吗?如果可以,该怎么做?我没有看到任何设置填充或边距以使其一直向左对齐的内容。
我终于得到了这个。您需要使用自定义操作栏视图。其实很简单:
(这是使用 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));
基本上就是这样!如果我遗漏了什么,请告诉我。
拥有可定制的操作栏视图有一些很好的好处,只要坚持基础知识,它就会看起来很棒。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)