Changing android actionbar button style

早过忘川 提交于 2019-12-19 04:56:18

问题


I'm currently changing the view on my ActionBar MenuItem like so:

MenuItem menuItem = menu.add(title);

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

menuItem.setActionView(tv);

However this disabled the function of the button, which I'm assuming is because I've changed the ActionView. Is there anyway I can do this whilst retaining the button function?


回答1:


Here's an example of creating a custom MenuItem for the ActionBar.

First: Create the View that will serve as your 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");
    }

}

Second: Create the layout that you'll actually apply to the 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>

It's important to include the style style="?android:attr/actionButtonStyle" to make sure the layout properly reflects ActionBar items. It's also important to include a android:contentDescription in your layout. Normally when you long press a MenuItem with an icon, a tooltip is shown to indicate what that MenuItem is for. In your case, you'll need to take an extra step to make sure this tooltip is still accessible.

A great helper for this is Roman Nurik's CheatSheet. You can see how I'm using it in the constructor of the action layout.

You specifically asked about the MenuItem being selectable. To do this, make sure you include the android:background="?android:attr/selectableItemBackground" and android:clickable="true" attributes.

Third: Use the android:actionLayout attribute to set your layout and inflate the menu normally within your Activity or Fragment.

your_menu

<item
    android:id="@+id/action_typeface"
    android:actionLayout="@layout/action_layout_example"
    android:showAsAction="ifRoom"
    android:title="@string/hello_world"/>

Using the MenuItem

In onCreateOptionsMenu call MenuItem.getActionView and on View.onClickListener. This is because onOptionsItemSelected will not be called for a custom View

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

The results




回答2:


try this

tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //handle
            }
        });


来源:https://stackoverflow.com/questions/19572049/changing-android-actionbar-button-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!