在自定义标题栏上设置标题文本

时间:2013-11-20 12:09:35

标签: android titlebar custom-titlebar

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

在这种情况下,默认情况下,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());

要动态更改文字,我还有活动

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

2 个答案:

答案 0 :(得分:1)

更改

title.setText(getTitle().toString());

答案 1 :(得分:0)

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