4

我正在使用 Sherlock 操作栏,并希望将显示为已启用的主页的图标从深色更改为白色。

但我的主题需要 Holo.Light。有什么办法可以通过样式为此设置自定义图标吗?

我在操作栏中使用以下操作项

main_activity.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:icon="@drawable/ic_menu_search"
        android:showAsAction="always"
        android:title="Search"/>
    <item
        android:id="@+id/action_category"
        android:icon="@drawable/ic_menu_category"
        android:showAsAction="always"
        android:title="Category"/>
    <item
        android:id="@+id/action_sort"
        android:icon="@drawable/ic_menu_sort"
        android:showAsAction="always"
        android:title="Sort">
        <menu>
            <item
                android:id="@+id/action_sort_dist_nf"
                android:icon="@android:drawable/ic_menu_search"
                android:title="Distance (Near-Far)"/>
            <item
                android:id="@+id/action_sort_dist_fn"
                android:icon="@android:drawable/ic_menu_search"
                android:title="Distance (Far-Near)"/>
            <item
                android:id="@+id/action_sort_alpha_az"
                android:icon="@drawable/ic_menu_sort_az"
                android:title="Alpha (A-Z)"/>
            <item
                android:id="@+id/action_sort_alpha_za"
                android:icon="@drawable/ic_menu_sort_az"
                android:title="Alpha (Z-A)"/>
        </menu>
    </item>

</menu>

活动地点.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_share"
        android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
        android:icon="@drawable/ic_menu_share"
        android:showAsAction="always"
        android:title="Search"/>

</menu>

我想设置搜索视图和搜索图标的样式。以及共享意图图标。

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

4

1 回答 1

4

像这样的自定义主题。

<style name="Theme.mTheme" parent="android:Theme.Holo.Light">
    <item name="android:homeAsUpIndicator">@drawable/image_with_whitecolorarrow</item>
    <item name="homeAsUpIndicator">@drawable/image_with_whitecolorarrow</item>
</style>

对于自定义搜索视图:

在菜单文件夹中定义菜单布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/searchIcon"
        android:actionLayout="@layout/search_icon_actionview"
        android:icon="@drawable/search_icon"
        android:showAsAction="ifRoom|collapseActionView"
        android:title="Search"/>

</menu>

searchIcon你可以使用你的自定义图标。但必须具有正确大小的操作栏中的 menuItems。

定义您的自定义 searchView 即..,search_icon_actionview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="5dp"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/search_image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentRight="true"
        android:clickable="true"
        android:contentDescription="Search icon"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:src="@drawable/search_icon" />

    <AutoCompleteTextView
        android:id="@+id/search_editText"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_toLeftOf="@id/search_image"
        android:background="@drawable/rounded_corners_white"
        android:hint="Search"
        android:imeOptions="actionSearch"
        android:padding="5dp"
        android:singleLine="true"
        android:textColor="@color/text_dark_grey"
        android:textColorHint="@color/text_light_grey"
        android:textSize="20dp" />

</RelativeLayout>

在此处输入图像描述

搜索图标和铃铛图标是我使用 photo shop 制作的自定义图像。

活动代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.searchIcon:

        mEtSearchbar.clearFocus();
        (new Handler()).postDelayed(new Runnable() {
            public void run() {
                mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(
                        SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(),
                        MotionEvent.ACTION_DOWN, 0, 0, 0));
                mEtSearchbar.dispatchTouchEvent(MotionEvent.obtain(
                        SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
                        0, 0, 0));
            }
        }, 100);

        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    inflater.inflate(R.menu.your_menu, menu);

    mSearchbar = (MenuItem) menu.findItem(R.id.searchIcon);
    View actionview = mSearchbar.getActionView();
    mEtSearchbar = ((AutoCompleteTextView) actionview
            .findViewById(R.id.search_editText));
    final ImageView searchImage = ((ImageView) actionview
            .findViewById(R.id.search_image));
    searchImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            String s = mEtSearchbar.getText().toString();

            Intent intent = new Intent(CurrentActivity.this,
                    SearchActivity.class);
            intent.putExtra("search_string", s);
            startActivity(intent);
        }
    });

    mEtSearchbar
            .setOnEditorActionListener(new TextView.OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_SEARCH) {

                        searchImage.performClick();
                        return true;
                    }
                    return false;
                }
            });

    super.onCreateOptionsMenu(menu, inflater);
}
于 2012-12-21T13:48:07.377 回答