简体   繁体   中英

Android: Action bar home button customize

I am developing an application in which what the need in action bar is shown in image:

在此处输入图片说明

I need to apply back arrow with text and search icon custom from drawable. My API level is from min 13 to 21 max. I searched on Google and found to do this from style.xml as:

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
<item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
</style>

But its not working.

I also tried :

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.drawable.back_button);

This gives me error as :

04-07 13:40:49.465: E/ACRA(20136): com.demo.example fatal error : android.app.ActionBar.setHomeAsUpIndicator
04-07 13:40:49.465: E/ACRA(20136): java.lang.NoSuchMethodError: android.app.ActionBar.setHomeAsUpIndicator
04-07 13:40:49.465: E/ACRA(20136):  at com.demo.example.TabbarActivity.activitySearchView(TabbarActivity.java:225)
04-07 13:40:49.465: E/ACRA(20136):  at com.demo.example.TabbarActivity.onCreate(TabbarActivity.java:157)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.Activity.performCreate(Activity.java:5206)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-07 13:40:49.465: E/ACRA(20136):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 13:40:49.465: E/ACRA(20136):  at android.os.Looper.loop(Looper.java:137)
04-07 13:40:49.465: E/ACRA(20136):  at android.app.ActivityThread.main(ActivityThread.java:4921)
04-07 13:40:49.465: E/ACRA(20136):  at java.lang.reflect.Method.invokeNative(Native Method)
04-07 13:40:49.465: E/ACRA(20136):  at java.lang.reflect.Method.invoke(Method.java:511)
04-07 13:40:49.465: E/ACRA(20136):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-07 13:40:49.465: E/ACRA(20136):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-07 13:40:49.465: E/ACRA(20136):  at dalvik.system.NativeStart.main(Native Method)

What i am missing or doing wrong. Please suggest me what should i do to achieve my task.

Using AppCompat is better option but still if you dont want to use it, set custom view in actionbar. This might be helpful when you are using custom fonts in text.

See this Reference for more info.

Like this,

LinearLayout customView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_actionbar_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL);
getActionBar().setCustomView(customView, params);

Hope it will help you!

setHomeAsUpIndicator是API级别18引入的,您遇到崩溃的设备的Android版本早于18。要解决该问题,您可以使用appcompat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM