まず、同様の問題に対応する多くの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
要素に追加すると、タイトルは
と表示されます)が表示されます。ツールバー/アクションバーので、私はコードでそれを行うことができるようにしたいと思います。
これは間違っているかもしれない場所を誰が見るのか?
これは上記と同じですか? 'ActionBar actionBar = getSupportActionBar(); if(actionBar!= null){actionBar.setTitle( "Hello World!"); } ' – Brett
あなたは次のテーマを使用していますか? –
John
@Brett:私は同じxmlとコードを使用し、 "Hello world!"と表示しました。 com.android.support:appcompat-v7 23.0.0、 – John