1

アクションバー(シャーロック)に現在のチャプター番号を表示したいアプリがあります。私のmenu.xmlは次のようになります:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/TextView01"
        android:title=""/>

</menu>

次のコードを使用すると、NullPointerException が発生します。

titleView = (TextView) findViewById(R.id.TextView01);
titleView.setText(chapterno);

sherlock アクション バーにテキストを表示し、動的に更新するにはどうすればよいでしょうか。

ActionBar は次のようになります。

ここに画像の説明を入力

4

1 に答える 1

4

これを実現するには、メニュー項目ではなく、アクション バーでカスタム レイアウトを使用する必要があります。

onCreate()

ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.actionbar_number);

レイアウトは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_marginRight="@dimen/marginMedium"
    android:gravity="center_vertical"
    android:text="0"
    android:textColor="@color/actionbar_number"
    android:textSize="28dp"
    android:textStyle="bold" />

番号を更新するには:

TextView chapterNumber = (TextView) getSupportActionBar().getCustomView();
chapterNumber.setText(String.valueOf(number));

UPDATE メニューアクションアイテムを追加するには、通常どおりにできるはずですが、カスタムレイアウトの配置がメニューアイテムと重なったり、アクションバーに表示される場合は非表示になったりする可能性があることに注意してください。

menu.xml

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

    <item
        android:id="@+id/menu_share"
        android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider"
        android:icon="@drawable/ic_menu_share"
        android:showAsAction="ifRoom"
        android:title="@string/share"
        android:visible="false"/>

</menu>

次に、あなたの活動で。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.menu_share);
    ShareActionProvider shareActionProvider = (ShareActionProvider) item.getActionProvider(); //line 387

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    shareIntent.setType("text/plain");

    shareIntent.putExtra(Intent.EXTRA_TEXT, "Test");
    shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Test");

    shareActionProvider.setShareIntent(shareIntent);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        onBackPressed();
        break;

    case R.id.menu_share:
        // EXAMPLE OF WHAT YOU CAN DO
        //          Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        //          sharingIntent.setType("image/png");
        //          sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(f));
        //          //sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
        //          //sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Body");
        //          startActivity(Intent.createChooser(sharingIntent, "Share via"));

        break;

    default:
        break;

    }
    return super.onOptionsItemSelected(item);
}
于 2013-06-14T03:31:31.780 に答える