工具栏和操作栏未显示在活动中 [英] Toolbar and ActionBar not shown in activity

查看:90
本文介绍了工具栏和操作栏未显示在活动中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先在我的应用程序中使用Material design,但问题是当我创建活动时没有操作栏或工具栏,但是我有Material Navigation抽屉,为什么它没有像旧版本中那样自动实现 活动布局:

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

} ` 我的主题:

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

推荐答案

制作如下custom_toolbar.xml文件:

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"/>

在您的onCreate方法中:

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

并保留清单文件:

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

希望有帮助!

这篇关于工具栏和操作栏未显示在活动中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆