Title, etc. not showing in Android Toolbar

倾然丶 夕夏残阳落幕 提交于 2019-11-26 21:31:07

问题


I've just added an Android Toolbar to an app I'm working on. The toolbar placement works correctly with the toolbar layout appearing at the top of the display. However, the toolbar is showing up as a blank colored bar...the App Title and Overflow Icon do not show up in the Toolbar. Any ideas on how to fix?

ToolbarActivity.java:

public abstract class ToolbarActivity extends ActionBarActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); 
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    toolbar.setTitle("app name");
    }
}

toolbar.xml:

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

menu_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="100"
    app:showAsAction="always" />
</menu>

style.xml:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
      <item name="windowActionBar">false</item>
      <item name="android:windowNoTitle">true</item>
      <item name="colorPrimary">@color/ColorPrimary</item>
      <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
</style>

回答1:


EDIT: Ok so this needs to be set in a completely different way to work. I assumed (because I hadn't worked with the ToolBar much yet, that when using setActionBar(toolbar) it added the custom ToolBar to the content view automatically - but that wasn't the case.

So the big issue is that the ToolBar is never added to the current content view and hence will never be visible until you actually add the ToolBar to the current content view.

There are different approaches, but I'm only gonna show one of the them.

Instead of having a toolbar.xml file, you should add the ToolBar directly into the layout file of the Activity.

Here's an example of the Activity class and the XML to go together with the Activity:

MainActivity.java

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if(toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("My custom toolbar!");
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light"
        android:minHeight="100dp">

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

</RelativeLayout>

Your styles.xml file is correct as it is.

It's important to note, that after setting your ActionBar to the custom ToolBar, you use the default methods for changing how the ActionBar looks like. For instance setting the title, should be set from getSupportActionBar().setTitle() and not from toolbar.setTitle().




回答2:


Tutorial: http://www.viralandroid.com/2015/08/android-toolbar-example.html

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    ...
  <!-- Customize your theme here. -->
</style>

layout file

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.Toolbar>

java

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);



回答3:


In manifest, add the following...

 <activity android:name=".Activity.ToolbarActivity"
            android:label="@string/yourStringTitle"
            />

Hope this helps



来源:https://stackoverflow.com/questions/28265208/title-etc-not-showing-in-android-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!