12

私はABSバージョンを使用しています。4そして、アクションモードの閉じるアイコンのほかに表示されるデフォルトの「完了」テキストを変更する必要がありますが、実際にはその方法がわかりません。

少なくとも2つの理由から、テキストはカスタマイズ可能である必要があると思います。

  • 「完了」はすべてのコンテキストに適しているわけではありません(たとえば、「キャンセル」の方が適切な場合があります。GalaxyTabの「マイファイル」アプリなど、いくつかのアプリを使用しています)

  • 「完了」は、ユーザーの言語に応じてローカライズする必要があります

そのテキストをカスタマイズすることは可能ですか?もしそうなら、誰かがそれを行う方法を教えてもらえますか?

前もって感謝します。

編集

一時的な回避策を見つけました。これを次のように投稿します。

private TextView getActionModeCloseTextView() {
    // ABS 4.0 defines action mode close button text only for "large" layouts
    if ((getResources().getConfiguration().screenLayout & 
        Configuration.SCREENLAYOUT_SIZE_MASK) == 
        Configuration.SCREENLAYOUT_SIZE_LARGE) 
    {
        // retrieves the LinearLayout containing the action mode close button text
        LinearLayout action_mode_close_button =
            (LinearLayout) getActivity().findViewById(R.id.abs__action_mode_close_button);
        // if found, returns its last child 
        // (in ABS 4.0 there is no other way to refer to it, 
        // since it doesn't have an id nor a tag)
        if (action_mode_close_button != null) return (TextView)
            action_mode_close_button.getChildAt(action_mode_close_button.getChildCount() - 1);
    }
    return null;
}

それが私が思いついた方法です。ABS4.0の構造に大きく依存していることに注意してくださいabs__action_mode_close_item.xml

これは私のシナリオでは機能しますが、ご覧のとおり、実際の「答え」に昇格させるのに十分満足できるとは言えないため、以前の投稿のみを編集しました。

それが他の誰かに役立つことを願っていますが、誰かがより良い、よりクリーンなソリューションを共有できることも願っています。

4

6 に答える 6

10

テーマを使用して、デフォルトのアイコンをオーバーライドできます。

    <item name="actionModeCloseDrawable">@drawable/navigation_back</item>
    <item name="android:actionModeCloseDrawable">@drawable/navigation_back</item>
于 2012-07-12T15:12:03.083 に答える
7

以前の ICS と >ICS の両方で、閉じるボタンの色とフォント サイズをカスタマイズできるように PacificSky のコードを編集しました。

という名前のメソッドを作成しましたcustomizeActionModeCloseButton

private void customizeActionModeCloseButton() {
      int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");    
      View v = getGSActivity().findViewById(buttonId);
      if (v == null) {
         buttonId = R.id.abs__action_mode_close_button;
         v = getGSActivity().findViewById(buttonId);
      }
      if (v == null)
         return;
      LinearLayout ll = (LinearLayout) v;
      if (ll.getChildCount() > 1 && ll.getChildAt(1) != null) {
         TextView tv = (TextView) ll.getChildAt(1);
         tv.setText(R.string.close_action_mode);
         tv.setTextColor(getResources().getColor(R.color.white));
         tv.setTextSize(18);
      }
   }

呼び出した直後に呼び出しますstartActionMode()

public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
   actionMode = getActivity().startActionMode(this);
   customizeActionModeCloseButton();
   return true;
}
于 2012-10-31T11:59:44.347 に答える
6

しばらく時間が経ちましたが、これは少しハッキーでない解決策です - 後世のために公開します。

Android 版 < ICS の場合

アプリケーションの strings.xml に次の行を追加します。

<string name="abs__action_mode_done">Cancel</string>

これは、TextView の (ActionBarSherlock/res/layout-large/abs__action_mode_close_item.xml で定義されている) android:text 属性をオーバーライドします。

Android バージョン ICS 以降の場合

ネイティブの ActionBar 機能は、ICS 以降で使用されます。次のコードを使用して、完了ボタンに関連付けられた文字列を見つけてオーバーライドする必要があります。

int buttonId = Resources.getSystem().getIdentifier("action_mode_close_button", "id", "android");
if (buttonId != 0)
{
    View v = findViewById(buttonId);
    if (v != null)
    {
        LinearLayout ll = (LinearLayout)v;
        View child = ll.getChildAt(1);
        if (child != null)
        {
            TextView tv = (TextView)child;
            tv.setText(R.string.cancel);
        }
    }
}
于 2012-10-23T23:50:00.747 に答える
3

PacificSkyの回答に感謝します。私の場合は便利です。

ここで説明する必要があるのは、o関数で呼び出されるなど、場合によってfindViewById(buttonId)は返される可能性があることです。これは、その時点ではforActionModeの閉じるボタンがまだ初期化されていないためです。nullnCreateActionMode()LinearLayout

アクションモードの閉じるボタンを非表示にしたいので、 200ミリ秒後にPacificSkyに電話をかけますsendEmptyMessageDelayedonCreateActionMode()わたしにはできる。

于 2012-11-01T11:51:18.563 に答える