setting menu item's title appears as all cap o

2019-08-28 17:48发布

问题:

I have a menu item as explained in How do I add a button as a menu item?. When I use an icon, I can click on the icon fine. But when I use an actionLayout, I am getting no response on click. Here is the button the actionLayout points to:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/my_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_marginRight="15dp"
        android:background="#999999"
        android:text="Apple" />

</RelativeLayout>

Any ideas how to fix this problem? The idea is to use my own view instead of a drawable.

回答1:

I cannot speak from experience but I have found this code fragment that might help you. (I believe the actionbar item is an icon first an actionbar when activated)

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.options, menu);
  MenuItem menuItem = menu.findItem(R.id.actionItem);
  ...

  menuItem.setOnActionExpandListener(new OnActionExpandListener() {
  @Override
  public boolean onMenuItemActionCollapse(MenuItem item) {
      // Do something when collapsed
      return true;  // Return true to collapse action view
  }

  @Override
  public boolean onMenuItemActionExpand(MenuItem item) {
      // Do something when expanded
      return true;  // Return true to expand action view
  }
  });

}

Link: http://developer.android.com/guide/topics/ui/actionbar.html



回答2:

Actually I figure it out. I use

<Button  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_marginRight="15dp"
    android:background="#999999"
    android:text="Apple" />

with getActionView