1

在我的应用程序中,我正在使用setCustomView方法设置操作栏的自定义视图。此视图在中心包含蓝色背景颜色和活动标题。

这是我用于设置自定义视图的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="25dp"
    android:background="@color/darkblue" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        android:paddingTop="3dp"
        android:text="@string/app_name" />

</RelativeLayout>

这是在活动操作栏中添加它的代码:

actionBar=getActionBar();
actionBar.setCustomView(R.layout.custom_actionbar);
actionBar.setDisplayOptions(com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_CUSTOM);

现在,我使用以下代码将菜单项(搜索)添加到此自定义操作栏:

public boolean onCreateOptionsMenu(Menu menu) {
        com.actionbarsherlock.view.MenuInflater inflater=getSupportMenuInflater();
        inflater.inflate(R.menu.search, menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }

但是现在这个添加的菜单(搜索)项的背景不是蓝色的,它是操作栏默认的黑色。

这是我的菜单xml文件:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/search"
        android:showAsAction="always"
        android:icon="@drawable/action_search"/>
   </menu>

我该如何解决这个问题。

4

1 回答 1

1

i think your code is working...

enter image description here

Please compare:

package com.example.testaction;

import android.os.Bundle;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;

public class MainActivity extends SherlockActivity {

    private ActionBar actionBar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Sherlock___Theme_Light);
        setContentView(R.layout.activity_main);

        actionBar = getSupportActionBar();
        actionBar.setCustomView(R.layout.test_action);
        actionBar
                .setDisplayOptions(com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_CUSTOM);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

EDITED

Insted Of Above Code use Custome Theam Like below then your problem is solved:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- the theme applied to the application or activity -->
    <style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <!-- other activity and action bar styles here -->
    </style>

    <!-- style for the action bar backgrounds -->
    <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:background">@color/colour_blue</item>
        <item name="android:backgroundStacked">#FFFFFF</item>
        <item name="android:backgroundSplit">@color/colour_blue</item>
    </style>

</resources>

in Class:

package com.example.testaction;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setTheme(R.style.Sherlock___Theme_Light);
        setContentView(R.layout.activity_main);
        /*
         * actionBar = getSupportActionBar();
         * actionBar.setCustomView(R.layout.test_action); actionBar
         * .setDisplayOptions
         * (com.actionbarsherlock.app.ActionBar.DISPLAY_SHOW_CUSTOM);
         */
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

add this style in Android Manifest:

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActivityTheme" >

its look like:

enter image description here

for more detail look in to this artical.

于 2013-04-03T11:14:01.367 回答