在ActionBarSherlock上显示自定义按钮以展示滑动菜单

4
我是一名有用的助手,可以为您翻译文本。

我正在开发一款使用ActionBarSherlockSlidingMenu的Android应用程序。

现在,这是我展示打开菜单按钮的方法:

enter image description here

但是不想在左侧显示 <,并更改其图标为自定义图标。

enter image description here

这是我设置滑动菜单的方法:

private void setUpSlidingMenuAndActionBar()
{
    setBehindContentView(R.menu.menu);

    setSlidingActionBarEnabled(true);        

    slidingMenu = getSlidingMenu(); 
    slidingMenu.setMode(SlidingMenu.LEFT);
    slidingMenu.setSlidingEnabled(false);
    slidingMenu.setBehindOffset(100);

    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

如何去掉左侧的 < 图标并更改主页按钮图标?

当我点击该按钮时,会打开滑动菜单。

更新:

根据 blackbelt 的答案,这是我所做的:

styles.xml:

<style name="AppTheme.ActionBarStyle" parent="@style/Theme.Sherlock">
    <item name="android:homeAsUpIndicator">@drawable/but_menu</item>
    <item name="homeAsUpIndicator">@drawable/but_menu</item>
</style>

Manifest.xml:

<application
    [ ... ]
    android:theme="@style/AppTheme.ActionBarStyle" >

结果为:

结果:

enter image description here

我能看到绿色的安卓!!! 我不想看到它!!

2个回答

3

在样式中使用homeAsUpIndicator

 <item name="android:homeAsUpIndicator">@drawable/home_up_drawable</item>
 <item name="homeAsUpIndicator">@drawable/home_up_drawable</item>

我的样子如下:
<style name="AppTheme.ActionBarStyle" parent="@style/Widget.Sherlock.ActionBar.Solid">
    <item name="android:homeAsUpIndicator">@drawable/home_up_drawable</item>
    <item name="homeAsUpIndicator">@drawable/home_up_drawable</item>
    <item name="android:titleTextStyle">@style/AppTheme.TitleTextStyle</item>
    <item name="android:background">@drawable/actionbar_background</item>
    <item name="background">@drawable/actionbar_background</item>
    <item name="android:displayOptions">showHome|useLogo</item>
    <item name="displayOptions">showHome|useLogo</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="windowContentOverlay">@null</item>
</style>

谢谢您的回答,但我对此非常陌生。我应该在哪里添加它? - VansFannel
在styles.xml文件中,android:前缀的是用于本地Action Bar支持的项目,而没有android:前缀的则是用于Sherlock的。 - Blackbelt
我已经更新了我的问题并提供了更多细节。使用你的答案,我仍然可以看到绿色的安卓机器人。 - VansFannel
2
我使用 getSupportActionBar().setIcon(android.R.color.transparent); 在运行时将其移除。 - Blackbelt
@blackbelt:你有没有办法将标题设置为页面的center_horizontal,就像示例中图片右侧一样,想要它居中显示? - Pinki
显示剩余2条评论

0

您可以制作自定义操作栏并更改其图标。

按照以下步骤进行:

只有通过创建自定义Sherlock标题栏才能实现。

为应用程序的标题创建单独的XML文件。

该文件包含如下内容...

在此自定义布局中,我们可以更改背景颜色和自定义图标。

只需将图像从drawable添加到header_sherlock.xml文件的按钮编码部分即可,例如android:background="@drawable/imageName"。

header_sherlock.xml

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
android:background="#008000"
 >

     <Button
    android:id="@+id/header_tvleft"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="left"
    android:layout_marginTop="@dimen/margintop"
    android:clickable="true"
    android:paddingLeft="@dimen/layoutpaddingleft"
    android:text="Back"
    android:background="@drawable/imageName"
    android:textColor="#ffffff" 
    android:textSize="@dimen/header_leftbtn"
     /> 


<Button
    android:id="@+id/header_tvcenter"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:layout_marginTop="@dimen/margintop"
    android:layout_marginRight="@dimen/sherlockpaddingright"   
    android:layout_marginLeft="@dimen/sherlockpaddingleft"
    android:text="Center"
    android:textColor="#ffffff"
    android:textStyle="bold"
    android:background="@drawable/save1"
    android:textSize="@dimen/header_title"
     />

    <Button
    android:id="@+id/header_tvright"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="right"
    android:layout_marginTop="@dimen/margintop"
    android:clickable="true"
    android:paddingRight="@dimen/layoutpaddingright"
    android:text="Right"
    android:textColor="#ffffff"
    android:background="@drawable/save1"
    android:textSize="@dimen/header_rightbtn"
     />

 </LinearLayout>

对于MainActivity或您想要添加标题的任何活动:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.header_sherlock);
header_tvleft = (Button) findViewById(R.id.header_tvleft);
header_tvleft.setText("LeftTitleName");
header_tvcenter = (Button) findViewById(R.id.header_tvcenter);
header_tvcenter.setText("CenterTitleName");
header_tvright = (Button) findViewById(R.id.header_tvright);
header_tvright.setText("RightTitleName");

then you will the listener code for button like this..

// 在这个按钮监听器代码中,我们可以设置滑动菜单编码....

 header_tvleft.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
    // inside you will able to set your sliding menu whatever.......                

        }
    });

注意:这些仅适用于创建自定义操作栏...
如果您需要更多澄清,请告诉我,我很乐意回答您的问题...

嗨,当点击右侧按钮时如何打开右侧滑动菜单,左侧按钮则打开左侧菜单? - Gautam Vasoya
当使用滑动菜单时,没有两个独立的左侧和右侧菜单。当我们看到滑动菜单时,首先点击左侧菜单,然后菜单将在同一菜单上移动到右侧。然后,当您在右侧再次点击相同的菜单时,它将再次出现在左侧..就是这样。 - harikrishnan
看看任何一个示例侧边栏程序,可能有助于理解。 - harikrishnan
谢谢,但现在我已经解决了我的问题。在我的情况下,我有两个滑动菜单。一个在左边,另一个在右边,就像Facebook一样。 - Gautam Vasoya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接