我的应用程序有一个自定义主页图标,我希望它一直对齐到操作栏的左侧,以便它接触到屏幕的边缘。这是否可能,如果可能,如何实现?我没有看到任何设置填充或边距以使其一直向左对齐的内容。
最佳答案
我终于成功了。您需要使用自定义操作栏 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 有一些不错的好处,只要坚持基本原则,它就会看起来很棒。
关于android - 如何去除 ActionBar 上应用程序图标和屏幕边缘之间的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9265927/