简体   繁体   中英

Android ActionBar item with Text Only

I want to place an item in the actionbar that has only text. I am using ActionBarSherlock. How can i do this. One is define an icon with a text. I would not prefer to do that . Is it possible to have a text item from my string resources ?

Just don't define the

android:icon

field in the menu.xml for that item.

Just figured this out, make sure to add android:showAsAction="always"

<item 
    android:id="@+id/menu_item"
    android:showAsAction="always"
    android:title="@string/menu_title" />

For future reference, I found that one must define the showAsAction attribute in both the http://schemas.android.com/apk/res/android namespace and the http://schemas.android.com/apk/res-auto namespace, to provide compatibility with different Android versions. Thus, your menu.xml file should look like:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="net.taptools.android.sportshistory.PlayerProfileActivity" >
<item android:id="@+id/action_settings"
    android:title="Statistics"
    app:showAsAction="always"
    android:showAsAction="always"/>
</menu>

if you want it to be a Button you can create a menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context="net.taptools.android.sportshistory.PlayerProfileActivity" >
<item android:id="@+id/action_settings"
    android:title="Statistics"
    app:showAsAction="always"
    android:showAsAction="always"/>
</menu>

But in case you wanted only a TextView you can use :

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    TextView tv = new TextView(this);
            tv.setText(getString(R.string.matchmacking)+"  ");
            tv.setTextColor(getResources().getColor(R.color.WHITE));
            tv.setOnClickListener(this);
            tv.setPadding(5, 0, 5, 0);
            tv.setTypeface(null, Typeface.BOLD);
            tv.setTextSize(14);
            menu.add(0, FILTER_ID, 1, R.string.matchmacking).setActionView(tv).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

This just makes a TextView .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM