Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
857 views
in Technique[技术] by (71.8m points)

android - Toolbar and ActionBar not shown in activity

I use Material design firstly in my apps and my problem is when I create activity there is no action bar or toolbar but I have Material Navigation drawer why it not implemented automatically like in older versions Layout of activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView4" />

</LinearLayout>

Activity: package fragments;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;


  public class Noname1 extends ActionBarActivity{

   @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
      setContentView(R.layout.test);

   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

     getMenuInflater().inflate(R.menu.main, menu);
     return super.onCreateOptionsMenu(menu);
 }

} ` My theme:

  <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/nliveo_green_colorPrimary</item>
    <item name="colorPrimaryDark">@color/nliveo_green_colorPrimaryDark</item>
    <item name="colorAccent">@color/nliveo_green_colorPrimary</item>
    <item name="android:textColorPrimary">@color/myTextPrimaryColor</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="android:windowBackground">@color/myWindowBackground</item>
</style>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make a custom_toolbar.xml file like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@color/nliveo_green_colorPrimary"
    android:layout_height="?android:attr/actionBarSize" />

Include it in your layout file wherever you want like:

<include android:id="@+id/toolBar" layout="@layout/custom_toolbar"/>

in Your onCreate Method:

public class Noname1 extends ActionBarActivity{

   @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.test);
     Toolbar tb = (Toolbar)findViewById(R.id.toolBar);
     setSupportActionBar(tb);
   }
}

And keep your manifest file:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/nliveo_green_colorPrimary</item>
    <item name="colorPrimaryDark">@color/nliveo_green_colorPrimaryDark</item>
    <item name="colorAccent">@color/nliveo_green_colorPrimary</item>
    <item name="android:textColorPrimary">@color/myTextPrimaryColor</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

    <item name="android:windowBackground">@color/myWindowBackground</item>
</style>

Hope it helps!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...