2016-09-08 8 views
-1

私は、アクションバーのタイトルを任意のテキストにプログラマチックに変更したいと考えています。appcompat v7ライブラリのアクションバータイトルの変更方法

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View actionBarview = inflater.inflate(R.layout.action_bar, null); 

    ActionBar actioBar = getSupportActionBar(); 
    actioBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 
    actioBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    actioBar.setCustomView(actionBarview, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 
    final DrawerLayout drowerlayout = (DrawerLayout) findViewById(R.id.drawerlayout); 

私が試した:私はここにAPPCOMPAT V7 を使用 は私のコードですactioBar.setTitle( "のMyText");

何も起こりませんでした。

<?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="48dp" 
    android:layout_gravity="right" 
    android:background="#bfbfbf" 
    android:gravity="right" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/TextView01" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0.1"/> 

    <TextView 
     android:id="@+id/txtBName" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="......" 
     android:textColor="#000" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0.1"/> 

    <Button 
     android:id="@+id/btnMenu" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="menu" /> 

は、ここに私のカスタムアクションバーのxmlです。

答えて

1

アクションバーのタイトル設定setTitle()が機能しないカスタムビューを設定しているため、デフォルトのアクションバービューでカスタムを削除してスティックするか、またはカスタムのTextViewが必要ですあなたのタイトルが何であれ、参照を取得してそのテキストを設定することができます。設定のCustomViewがここにあなたのケースのために必要な場合

あなたは

View actionBarview = inflater.inflate(R.layout.action_bar, null); 
TextView tvTitle = (TextView) actionBarview.findViewById(R.id.tv_title) 
tvTitle.setText(<your title here>) 

tv_titleTextViewウィジェットとしてカスタムビューのXMLにする必要があります覚えている...それについて移動する必要があります方法です。後でコードでtvTitleを参照して、アクションバーを変更することができます。タイトル

+0

ありがとう、私は質問を更新し、XMLコードを追加しました。私はtxtBNameにテキストビューのIDを設定するこれはあなたの意味ですか? – Alfi

+0

それは完璧に機能しました!ありがとうございました! – Alfi

+1

いつでも仲間にして、actionBarのカスタムビューを使用しながらGoogle標準に準拠するようにしてください。また、actionBarの代わりにactionBarの代わりにToolbarsを使用する方法を採用してください –

関連する問題