1

ビューのメイン部分で特定のことが起こったときに、カスタムのActionBarタイトルを動的に変更しようとしています。そして、私は最初に次のコードを使用して、正しくタイトルを設定することができていたにも関わらず...カスタムActionBarのタイトルを動的に変更する方法は?

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.activity_scanning_title); 

私は必要なときにそれを修正するためにアクションバーから右のオブジェクトへのアクセスを得るように見えることはできません。直感的なオプションが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はここにある:それを得るために、カスタムView

<TextView 
    android:id="@+id/title" 
    ... /> 

コールfindViewById()

<?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> 

答えて

1

TextViewにIDを付け

TextView title = (TextView) bar.getCustomView().findViewById(R.id.title); 

setText() mあなたのタイトルを変更する方法:

title.setText("New title"); 
+0

本当に正解です。そして非常に徹底したものでもあります。チャンピオンのように働いた。 – Alyoshak

関連する問題