首先,我知道有很多SO解決了類似的問題。我已經把他們全部搜索過了,沒有一個提供的答案正在幫助解決我的問題。Android ActionBar不顯示在代碼中設置的標題
我無法獲得標題(或後退按鈕)出現在我的android.support.v7.widget.Toolbar
組件上。無論我嘗試過什麼。
我有一個Android的活動,看起來像(my_activity.xml):
<?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>
和As(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 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