0

我必须创建一个自定义标题栏,以设置动态文本。这类似于在操作栏上设置标题,以及下面的动态字幕。

在这种情况下,App 标题默认位于左侧。动态文本位于右侧,并且必须动态更改。

这是布局custom_titlebar.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="vertical" >

<TextView 
    android:id="@+id/title_left_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="left" />

<TextView 
    android:id="@+id/title_right_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="right"
    android:layout_alignParentRight="true" />    
</RelativeLayout>

在我定义的清单中:

<application
    android:theme="@style/Theme.NoTitle" >

活动上:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        //HAVE COMENTED THIS, BECAUSE IT THROWS: You cannot combine custom titles with other title feature..

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_titlebar);

    TextView title = (TextView) findViewById(R.id.title_left_text);
    title.setText(getTitle());

要动态更改文本,我还有Activity

public void setTitleStatus(String right) {
    if (right.length() > 20) {
        right = right.substring(0, 20);
    }
    TextView status = (TextView) findViewById(R.id.title_right_text);

    status.setText(right);
}

问题是在我设置标题(title.setText(getTitle());)的 onCreate 行上抛出了一个 NPE

4

2 回答 2

1

改变

title.setText(getTitle().toString());
于 2013-11-20T13:05:22.470 回答
0

查看 Android 示例。在蓝牙项目中制作了一个自定义操作栏,可以在不同的场合改变它的状态

于 2013-11-20T12:13:38.867 回答