android 4.0,操作栏上的文本从不显示 [英] android 4.0, text on the action bar NEVER shows

查看:43
本文介绍了android 4.0,操作栏上的文本从不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用来自 google 的新 API,特别是操作栏.

I am trying to use the new api's from google, specifically the action bar.

当构建设置为 api 10 时,如果我按下菜单按钮,我会看到漂亮的菜单选项,每个选项都有图片和图标.使用 api 14 时,无论我尝试什么,它总是将图标放在没有文本的操作栏中.我已经尝试了所有我能想到的.我给了它with text"属性,将文本更改为单个字符(以防它是房间问题),但没有.

When the build was set at api 10, if I pressed the menu button, I got nice looking menu options, each with a picture and icon. When using api 14, No matter what I try, it always puts the icon in the action bar with NO text. I have tried everything I can think of. I gave it the "with text" property, changed the text to a single character (in case it was a room issue), but nothing.

我以前见过这样做,即使在 android.developer 的开发者指南中,但我似乎无法找到有关如何让它显示的答案.

I have seen this done before, even in the developer guide at android.developer, but I can't seem to find an answer as to HOW to get it to show up.

推荐答案

我怀疑 Android 开发人员有意识地决定永远不要在狭窄的操作栏上显示单个菜单项的文本和图标.但如果你真的想这样做,你可以使用 android:actionLayout 在您的 menu.xml 文件中.Android ActionBar 文档 有一个更好的解释.

I suspect that it was a conscious decision by the Android developers to never display a single menu item's text and icon on a narrow action bar. But if you really want to do so, you can use android:actionLayout in your menu.xml file. The Android ActionBar documentation has a slightly better explanation.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_foo"
          android:title="@string/menu_foo"
          android:icon="@drawable/ic_menu_foo"
          android:showAsAction="always"
          android:actionLayout="@layout/action_button_foo" />
</menu>

然后创建您的 action_button_foo.xml 布局:

Then create your action_button_foo.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="14dp"
    android:paddingBottom="14dp"
    android:gravity="center"
    android:text="@string/menu_foo"
    android:drawableLeft="@drawable/ic_menu_foo"
    android:background="@drawable/bg_btn_action_bar"
    android:clickable="true" />

并为其背景使用选择器bg_btn_action_bar.xml,所以当你点击它时它会改变颜色:

and use a selector for its background bg_btn_action_bar.xml, so it changes color when you tap it:

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/bg_action_bar_pressed" />
    <item
        android:drawable="@color/transparent" />
</selector>

现在你需要让你的自定义视图处理点击事件.在您的 Activity 中,我喜欢这样做,以便我可以处理 onOptionsItemSelected 中的点击以及所有其他非自定义项目.

Now you'll need to make your custom view handle click events. In your Activity, I like to do this, so that I can handle the click in onOptionsItemSelected along with all my other, non-custom items.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.my_menu, menu);

    final MenuItem item = menu.findItem(R.id.menu_foo);
    item.getActionView().setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onOptionsItemSelected(item);
        }
    });

    return super.onCreateOptionsMenu(menu);
}

这篇关于android 4.0,操作栏上的文本从不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆