简体   繁体   English

Sherlock操作栏中的TextView

[英]TextView in Sherlock action bar

I have an app where I want to display the current chapter number in the action bar (sherlock). 我有一个应用程序,我想在操作栏(sherlock)中显示当前章节号。 My menu.xml looks like: 我的menu.xml看起来像:

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

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

</menu>

I am getting NullPointerException when using the following code: 使用以下代码时,我收到NullPointerException:

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

Any idea, how can we show text in sherlock action bar and update it dynamically. 任何想法,我们如何在sherlock动作栏中显示文本并动态更新。

The ActionBar looks like: ActionBar看起来像:

在此输入图像描述

You have to use a custom layout in the action bar and not a menu item to achieve this. 您必须在操作栏中使用自定义布局而不是菜单项才能实现此目的。

in onCreate() onCreate()

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

Your layout should be: 你的布局应该是:

<?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" />

To update the number: 要更新号码:

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

UPDATE To add menu action item you should be able to do that as normal, just be careful, the positioning of your custom layout may overlap menu items or hide then if they appear in the action bar. 更新要添加菜单操作项,您应该能够照常执行此操作,请注意,自定义布局的位置可能与菜单项重叠,或者如果它们出现在操作栏中则隐藏。

in menu.xml 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>

Then in your activity. 然后在你的活动中。

@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);
}

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

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