问题
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.
回答1:
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!
回答2:
setHomeAsUpIndicator was introduced with API level 18, and the device where you are experiencing the crash has a version of android older than 18. To overcome it you can use appcompat
来源:https://stackoverflow.com/questions/29486908/android-action-bar-home-button-customize