CAC*_*lan 7 android android-3.0-honeycomb android-actionbar
我的应用程序有一个自定义主页图标,我希望它一直对齐到操作栏的左侧,这样它就会触及屏幕的边缘.这是可能的,如果是的话,怎么办呢?我没有看到任何设置填充或边距使其一直向左对齐的东西.
我终于成功了.您需要使用自定义操作栏视图.这实际上非常简单:
(这是使用ActionbarSherlock,它应该与股票compat库一起工作......)
首先在res/values/themes.xml中,将操作栏显示选项设置为"custom":
<item name="android:displayOptions">showCustom</item>
Run Code Online (Sandbox Code Playgroud)然后创建一个名为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. -->
Run Code Online (Sandbox Code Playgroud)
接下来,在您的活动代码中添加以下内容:
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));
Run Code Online (Sandbox Code Playgroud)基本上就是这样!如果我遗漏了什么,请告诉我.拥有可自定义的操作栏视图有一些很好的好处,只需坚持基础,它看起来很棒.
