工具栏不可见AppCompat v7级别22

时间:2015-11-20 19:47:54

标签: android android-appcompat android-toolbar

我在活动中使用这样的工具栏

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

这是我的风格

 <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

问题

如果我使用windowNoTitle“TRUE”,主活动中没有工具栏。如果我使用windowNoTitle“FALSE”它会抛出异常

java.lang.IllegalArgumentException: AppCompat does not support the current theme features 

我在做什么错?

2 个答案:

答案 0 :(得分:0)

首先,擅长使用具有 ToolBar 而非 ActionBar 的最新设计(在材料设计中不推荐使用)。看看这个指南: https://github.com/codepath/android_guides/wiki/Using-the-App-ToolBar

你几乎就在那里;要停止发生异常,请从XML中删除<item name="windowActionBar">false</item>。主题已经没有动作栏了,所以你基本上都试图说'#34;没有动作栏&#34;两次,Android不喜欢并认为是非法的。

按照指南的其余部分显示工具栏。您可能只是错过了活动中的这些内容:

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        // Find the toolbar view inside the activity layout
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // Sets the Toolbar to act as the ActionBar for this Activity window.
        // Make sure the toolbar exists in the activity and is not null
        setSupportActionBar(toolbar);
    }

    // Menu icons are inflated just as they were with actionbar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
}

要特别注意setSupportActionBar(toolbar);,它告诉框架将toolbar视为操作栏。耶物质设计!

答案 1 :(得分:0)

  1. styles.xml文件的值
  2. <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppTheme" parent="AppTheme.Base">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>
    
    <!-- Application theme. -->
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowActionBar">false</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    

    1. styles.xml文件的值为-v21
    2. <!--
          Base application theme, dependent on API level. This theme is replaced
          by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
      -->
      <style name="AppTheme" parent="AppTheme.Base">
          <!--
              Theme customizations available in newer API levels can go in
              res/values-vXX/styles.xml, while customizations related to
              backward-compatibility can go here.
          -->
      </style>
      

        清单文件中的
      1. 机器人:主题= “@风格/ AppTheme”