Title, etc. not showing in Android Toolbar

白昼怎懂夜的黑 提交于 2019-11-27 23:35:10

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().

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);

In manifest, add the following...

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

Hope this helps

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