2016-11-10 83 views
0

首先,我知道有很多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 HereHello World!。但是,如果我将行bind:title="Testing"添加到XML的Toolbar元素,我看到标题显示为测试。但是,我希望能够操纵工具栏/动作条,所以我想能够做到这一点的代码。

有谁看到这可能是走错了?

回答

3

尝试设置labe l在你的AndroidManifest.xml中

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

否则,如果你想如果您在使用设置为编程

如果您在使用扩展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

相关问题