23

表明我有我有这么多的活动中使用与片段文件,但我的问题是,我不能显示我想要的工具栏中的文本的XML,我使用XML,那是因为我有一个导航抽屉,我需要处理一些事情,所以我必须这样做。Android的标题不会在工具栏

我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/frame_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".StatusActivity" 
    android:orientation="vertical" > 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     style="@style/ToolBarStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:minHeight="@dimen/abc_action_bar_default_height_material" /> 

</RelativeLayout> 

我的一个活动:

public class GroupChatActivity extends ActionBarActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_base_layout); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setTitle("Groups history"); 

     Aniways.init(this); 

     if(savedInstanceState == null) 
     { 
      FragmentManager manager = getSupportFragmentManager(); 

      Fragment fragment = GroupChatFragment.newInstance(getIntent().getIntExtra("uid", 0)); 
      manager.beginTransaction().add(R.id.frame_container, fragment).commit(); 
     } 
    } 
} 

,你可以看到我试着标题设置为操作栏,但它不工作。

+2

尝试调用'getSupportActionBar()setDisplayShowTitleEnabled(真)''然后toolbar.setTitle (title)' –

+0

@NikolaDespotoski试过了,它不起作用。 – bitek

回答

32
getSupportActionBar().setDisplayShowTitleEnabled(true); 
+1

对我来说,工作没有这在Android 5.1.1,即只有getSupportActionBar()的的setTitle工具栏里面的标题设置,但如果我尝试直接从工具栏设置标题引用没有,只能通过getSupportActionBar( ) – bitek

2

尝试toolbar.setTitle('Groups history');

0

getActionBar().setTitle("Groups History");

,或者如果你正在使用AppCompat库;

getSupportActionBar().setTitle("Groups History");

8

试试这个..这个方法对我的作品.. !!希望它可以帮助别人.. !!

<android.support.v7.widget.Toolbar 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/my_awesome_toolbar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="?attr/colorPrimary" 
android:minHeight="?attr/actionBarSize" 
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > 

<TextView 
    android:id="@+id/toolbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:singleLine="true" 
    android:text="Toolbar Title" 
    android:textColor="@android:color/white" 
    android:textSize="18sp" 
    android:textStyle="bold" /> 

</android.support.v7.widget.Toolbar> 

编辑

您也可以使用此功能。

setSupportActionBar(toolbar); 
if (getSupportActionBar() != null) 
     getSupportActionBar().setTitle("Toolbar title"); 
+0

做这似乎是getSupportActionBar()功能剪短 – Pievis

+0

是的,但不是getSupportActionBar()不是对我的作品,对一些糟糕的情况下,我用这个,.. –

17

设置,

app:title="@string/my_title" 

工具条中的该android.support.v7.widget.Toolbar,硬编码标题的声明中。

以编程方式设置标题,

Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar); 
toolbar.setTitle("my title"); 
setSupportActionBar(toolbar); 
在活动类

+2

凭啥不'安卓title'不是抛出一些有什么错误?! –

+0

是的你正确:) thnx男子我忘了添加这一行:) –

+0

@DanielWilson因为谷歌 –

0

我做了一个自定义的操作栏。

与此代码

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:local="http://schemas.android.com/apk/res-auto" 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/blanco" 
android:minHeight="?attr/actionBarSize"> 

    <TextView 
    android:id="@+id/toolbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="left" 
    android:singleLine="true" 
    android:text="Toolbar Title" 
    android:textColor="@color/naranja" 
    android:textSize="18sp" /> 

</android.support.v7.widget.Toolbar> 

在我的课扩展AppCompatActivity布局iguepardos_action_bar.xml我有这样的:

protected void onCreate(Bundle savedInstanceState) { 
.... 
.... 

getSupportActionBar().setDisplayShowCustomEnabled(true); // is for specify use custom bar 
getSupportActionBar().setCustomView(R.layout.iguepardos_action_bar); // my custom layout 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Button for come back 

View mCustomView = getSupportActionBar().getCustomView(); // Set the view 
TextView TitleToolBar = (TextView) mCustomView.findViewById(R.id.toolbar_title); // find title control 
TitleToolBar.setText("The Title Show"); // Set the Title 

}