安卓4.0,操作栏上的文字从不示栏上、文字、操作

2023-09-12 01:02:47 作者:蒹葭苍苍

我想使用新的API从谷歌,特别是操作栏。

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

在构建设为API 10,如果我pressed菜单按钮,我得到了很好看的菜单选项,每一个画面和图标。当使用API​​ 14,无论我怎么努力,它总是把与任何文本的操作栏中的图标。我已经尝试了所有我能想到的。我给它的文本属性,改变了文本的单个字符(如果这是一个房间的问题),但没有。

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的动作条文档有一个稍微好一点的解释。

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 布局:

<?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>

现在你需要让你的自定义视图处理click事件。在你的活动,我想这样做,这样我可以处理单击 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);
}