簡體   English   中英

Android ActionBar項目,僅限文本

[英]Android ActionBar item with Text Only

我想在只有文本的操作欄中放置一個項目。 我正在使用ActionBarSherlock。 我怎樣才能做到這一點。 一個是定義帶有文本的圖標。 我不喜歡那樣做。 是否可以從我的字符串資源中獲取文本項?

只是不要定義

android:icon

該項目的menu.xml中的字段。

剛想出來,確保添加android:showAsAction =“always”

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

為了將來參考,我發現必須在http://schemas.android.com/apk/res/android命名空間和http://schemas.android.com/apk/res-auto命名空間中定義showAsAction屬性。 ,提供與不同Android版本的兼容性。 因此,您的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>

如果你想要它是一個按鈕,你可以創建一個菜單:

<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>

但是如果你只想要一個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);
}

這只是一個TextView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM