ビューの主要部分で特定のことが発生したときに、カスタム ActionBar のタイトルを動的に変更しようとしています。そして、次のコードを使用して、最初はタイトルを正しく設定できましたが...
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.activity_scanning_title);
必要に応じて変更するために、ActionBar から適切なオブジェクトにアクセスできないようです。直感的なオプションは getCustomView() だったので、次のようになりました。
ActionBar bar = getSupportActionBar();
android.view.View v = bar.getCustomView();
if (v instanceof android.widget.LinearLayout)
{
LinearLayout layt = (LinearLayout)v;
// ???
. . .
}
getCustomView() は実際に LinearLayout を返しますが、元のタイトルを適切に表示するレイアウトの一部である TextView を取得できません。変更する必要があるだけです。
カスタム タイトルを定義する LinearLayout の xml は次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Scan Assessment"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>