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 반환집니다,하지만 난 레이아웃의 일부인 텍스트 뷰에서 얻을 수없는, 원래 제목을 올바르게 표시 한 경우에만 변경해야합니다.

내 사용자 지정 제목을 정의하는있는 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

관련 문제