gpt4 book ai didi

Android Sherlock Action Bar - 样式化 SearchView、Share Intent Icon、homeAsUpIndicator

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:12:00 25 4
gpt4 key购买 nike

我正在使用 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>

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

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

最佳答案

像这样的自定义主题。

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

对于自定义搜索 View :

menu文件夹中定义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 中 menuItems 的正确大小。

定义您的自定义搜索 View ,即..,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

搜索图标和铃铛图标是我使用照片商店制作的自定义图像。

Activity 代码:

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

关于Android Sherlock Action Bar - 样式化 SearchView、Share Intent Icon、homeAsUpIndicator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991296/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com