Android的:行动起来吧home键自定义自定义、来吧、行动、Android

2023-09-12 23:41:52 作者:爱别人不如爱自己

我开发中有什么需要在操作栏中显示的图像的应用:

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

我需要申请回头箭与绘制文字和搜索图标定制。我的API级别为13分钟至21最大。我搜索谷歌和发现从style.xml做到这一点的:

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.

我也试过:

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.

推荐答案

使用AppCompat是更好的选择,但仍,如果你不想使用它,在动作条设置自定义视图。当你在文字使用自定义字体,这可能是有帮助的。

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.

与此类似,

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!

 
精彩推荐
图片推荐