更改android操作栏按钮样式

ing*_*.am 0 android styles button android-actionbar

我正在改变我ActionBar MenuItem喜欢的观点:

MenuItem menuItem = menu.add(title);

TextView tv = new TextView(context);
tv.setTypeface(myTypeface);
tv.setText("hello");

menuItem.setActionView(tv);
Run Code Online (Sandbox Code Playgroud)

然而,这禁用了按钮的功能,我假设是因为我改变了ActionView.无论如何我可以在保留按钮功能的同时做到这一点吗?

adn*_*eal 7

以下是创建一个自定义的例子MenuItemActionBar.

第一:创建View将作为你的MenuItem.

ActionLayoutExample

/**
 * A {@link RelativeLayout} that serves as a custom {@link MenuItem}
 */
public class ActionLayoutExample extends RelativeLayout {

    /** The MenuItem */
    private TextView mHello;

    /**
     * Constructor for <code>ActionLayoutExample</code>
     * 
     * @param context The {@link Context} to use
     * @param attrs The attributes of the XML tag that is inflating the view
     */
    public ActionLayoutExample(Context context, AttributeSet attrs) {
        super(context, attrs);
        // Ensure the MenuItem is accessible
        CheatSheet.setup(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        // Initialize the TextView
        mHello = (TextView) findViewById(android.R.id.text1);
        mHello.setTypeface(myTypeface);
        mHello.setText("Hello");
    }

}
Run Code Online (Sandbox Code Playgroud)

第二步:创建您实际应用于的布局MenuItem.

action_layout_example

<your.package.name.ActionLayoutExample xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/actionButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:contentDescription="@string/hello_world" >

    <TextView
        android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</your.package.name.ActionLayoutExample>
Run Code Online (Sandbox Code Playgroud)

包含样式style="?android:attr/actionButtonStyle"以确保布局正确反映ActionBar项目非常重要.android:contentDescription在您的布局中包含a也很重要.通常,当您长按一个MenuItem图标时,会显示工具提示以指示其MenuItem用途.在您的情况下,您需要采取额外步骤以确保仍可访问此工具提示.

对此有一个很好的帮手是Roman Nurik的CheatSheet.你可以在动作布局的构造函数中看到我是如何使用它的.

你明确询问了MenuItem可选择性.为此,请确保包含android:background="?android:attr/selectableItemBackground"android:clickable="true"属性.

第三步:使用该android:actionLayout属性设置您的布局并在您的Activity或中正常膨胀菜单Fragment.

your_menu

<item
    android:id="@+id/action_typeface"
    android:actionLayout="@layout/action_layout_example"
    android:showAsAction="ifRoom"
    android:title="@string/hello_world"/>
Run Code Online (Sandbox Code Playgroud)

使用 MenuItem

onCreateOptionsMenu通话MenuItem.getActionViewView.onClickListener.这是因为onOptionsItemSelected不会调用自定义View

final View example = menu.findItem(R.id.action_typeface).getActionView();
example .setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do something
    }
});
Run Code Online (Sandbox Code Playgroud)

结果

在此输入图像描述