Android Sherlock Action Bar - 样式SearchView,分享意图图标,homeAsUpIndicator

时间:2012-12-21 13:45:43

标签: android android-actionbar actionbarsherlock

我正在使用Sherlock Actionbar并希望将显示为家庭的图标更改为从黑暗变为白色。

但我的主题需要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>

activity_place.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>

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

enter image description here enter image description here enter image description here

1 个答案:

答案 0 :(得分: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>

对于自定义searchView:

菜单文件夹

中定义menulayout
<?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 您可以使用自定义图标。但必须在Actionbar中具有正确大小的menuItem。

定义您的自定义searchView ie .., 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>

enter image description here

搜索图标和铃声图标是我使用照相馆制作的自定义图像。

活动代码:

@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);
}