TextView的夏洛克行动起来吧 [英] TextView in Sherlock action bar

查看:154
本文介绍了TextView的夏洛克行动起来吧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我想显示在操作栏(​​福尔摩斯)当前章节号。我menu.xml文件是这样的:

I have an app where I want to display the current chapter number in the action bar (sherlock). My menu.xml looks like:

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

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

</menu>

我使用下面的code时,得到NullPointerException异常:

I am getting NullPointerException when using the following code:

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

任何想法,我们如何能显示福尔摩斯操作栏中的文本,并动态更新它。

Any idea, how can we show text in sherlock action bar and update it dynamically.

在动作条是这样的:

推荐答案

您必须使用自定义布局操作栏中,而不是一个菜单项来实现这一点。

You have to use a custom layout in the action bar and not a menu item to achieve this.

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

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

这篇关于TextView的夏洛克行动起来吧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆