我必须创建一个自定义标题栏,以设置动态文本。这类似于在操作栏上设置标题,以及下面的动态字幕。
在这种情况下,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