简体   繁体   English

Android:操作栏主屏幕按钮自定义

[英]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. 我的API等级是从13到21最大值。 I searched on Google and found to do this from style.xml as: 我在Google上进行了搜索,发现从style.xml中这样做是:

<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. 使用AppCompat是更好的选择,但是如果您不想使用它,请在操作栏中设置自定义视图。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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