ダイナミック テキストを設定するには、カスタム タイトル バーを作成する必要があります。これは、タイトルを設定し、その下にアクションバーの動的サブタイトルを設定するようなものです。
この場合、アプリのタイトルはデフォルトと同様に左側に表示されます。動的テキストは右側に入り、動的に変化する必要があります。
これはレイアウト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);
}
問題は、タイトルを設定した onCreate の行に NPE をスローしていることです ( title.setText(getTitle());
)