Sherlock操作栏中的TextView

时间:2013-06-14 03:14:28

标签: java android

我有一个应用程序,我想在操作栏(sherlock)中显示当前章节编号。我的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看起来像:

enter image description here

1 个答案:

答案 0 :(得分: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));

<强>更新 要添加菜单操作项,您应该能够照常执行此操作,请注意,自定义布局的位置可能与菜单项重叠,或者如果它们出现在操作栏中则隐藏。

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);
}
相关问题