2016-11-10 51 views
0

まず、同様の問題に対応する多くのSOがあることはわかっています。私はそれらをすべて洗ったし、提供された答えのどれもが私の問題を解決するのに役立っていない。Android ActionBarにコードで設定されたタイトルが表示されない

android.support.v7.widget.Toolbarコンポーネントにタイトル(または戻るボタン)が表示されません。私が何を試しても関係なく。

<?xml version="1.0" encoding="utf-8"?> 
<layout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:bind="http://schemas.android.com/apk/res-auto"> 
    <data> 
     <variable name="infos" type="com.mycompany.orm.adapters.ListViewAdapter"/> 
    </data> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="10"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/my_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="@color/buttonBackground" 
      android:elevation="4dp" 
      android:logo="@drawable/ic_close_white_24dp" /> 

     <ListView 
      android:id="@+id/my_listview" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:divider="@color/windowBackground" 
      bind:items="@{infos.list}"/> 
    </RelativeLayout> 
</layout> 

そして(MyActivity.java)としての活性onCreate

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.my_activity); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
    if (toolbar != null) { 
     toolbar.setTitle("My Name Here"); // Never shows up 
     setSupportActionBar(toolbar); 

     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM); 
      actionBar.setTitle("Hello World!");  // Never shows up 
      actionBar.setDisplayHomeAsUpEnabled(true); 
      actionBar.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp); 
      actionBar.setDisplayShowTitleEnabled(true); 
      actionBar.show(); 
     } 
    } 
} 

しかし、使用して

は、私はそれが( my_activity.xml)のように見えるのAndroid活性を有しこのコードは、私は空白のツールバーが表示されます。私はテキストを表示しません( My Name Hereまたは Hello World!ですが、 bind:title="Testing"という行をXMLの Toolbar要素に追加すると、タイトルは と表示されます)が表示されます。ツールバー/アクションバーので、私はコードでそれを行うことができるようにしたいと思います。

これは間違っているかもしれない場所を誰が見るのか?

答えて

3

labeを設定しようそうでなければあなたが使用している場合は、のように、プログラム

を設定したい場合に

<activity 
     android:name="yourpackagename.Activity" 
     android:label="My Name Here"/> 

のようなあなたののAndroidManifest.xmlのLは、あなたが使用している場合AppCompatActivityクラスの使用に

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    toolbar.setTitle("My Name Here"); 
    this.getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

を拡張Activityクラスの使用を拡張します

ActionBar ab = getActionBar(); 
    ab.setTitle("My Name Here"); 

上記のソリューションを私のために使ってみることをお勧めします。

0

私は同じ問題に直面していたし、これは私のために働いた。

setSupportActionBar(toolbar); 
getSupportActionBar().setTitle(""My Name Here""); 
+0

これは上記と同じですか? 'ActionBar actionBar = getSupportActionBar(); if(actionBar!= null){actionBar.setTitle( "Hello World!"); } ' – Brett

+0

あなたは次のテーマを使用していますか? John

+0

@Brett:私は同じxmlとコードを使用し、 "Hello world!"と表示しました。 com.android.support:appcompat-v7 23.0.0、 – John

関連する問題