简体   繁体   English

Android ActionBar项目,仅限文本

[英]Android ActionBar item with Text Only

I want to place an item in the actionbar that has only text. 我想在只有文本的操作栏中放置一个项目。 I am using ActionBarSherlock. 我正在使用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. 该项目的menu.xml中的字段。

Just figured this out, make sure to add android:showAsAction="always" 刚想出来,确保添加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. 为了将来参考,我发现必须在http://schemas.android.com/apk/res/android命名空间和http://schemas.android.com/apk/res-auto命名空间中定义showAsAction属性。 ,提供与不同Android版本的兼容性。 Thus, your menu.xml file should look like: 因此,您的menu.xml文件应如下所示:

<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 : 但是如果你只想要一个TextView你可以使用:

@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 . 这只是一个TextView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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