【问题标题】:Material Drawer In Eclipse IsseEclipse Isse 中的材料抽屉
【发布时间】:2015-04-28 07:18:47
【问题描述】:

我想在 android 应用程序中添加材料抽屉...所以我们需要我们必须包含一个工具栏.. 但是当我们包含工具栏时我得到了错误 我的 activitymain_xml 代码是

       <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_parent_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:fitsSystemWindows="true">

        <include layout="@layout/toolbar"/>


    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
           </FrameLayout>

        <!-- The navigation drawer -->
      <ListView android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#004869"
            android:textSize="20sp"
            android:dividerHeight="1dp"
            android:background="#fff"
            android:textColor="#f9f5f5"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"
            android:paddingTop="10dp"
            />

          </android.support.v4.widget.DrawerLayout>

         </LinearLayout>

工具栏的代码是

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

包含工具栏的代码是

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

我的 Style.xml 是

    <resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
     <item name="windowActionBar">false</item> 
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

    </style>

   </resources>

我的 Mainifiest 文件是

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.lifegoal.eshop"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="com.lifegoal.eshop.Splash_Screen_Activity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </manifest>

但是我遇到了类似的错误

   04-28 12:53:05.632: E/AndroidRuntime(3181): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lifegoal.eshop/com.lifegoal.eshop.Home_Screen_Activity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
   04-28 12:53:05.632: E/AndroidRuntime(3181):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
   04-28 12:53:05.632: E/AndroidRuntime(3181):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)

伙计们,我需要你的帮助 ..thx 提前

【问题讨论】:

  • 你能显示你的styles.xml吗?
  • 请发布您的 AndroidManifest.xml 和 styles.xml
  • 删除setSupportActionBar(toolbar);这一行后运行。
  • 您确定没有覆盖 values-vxx 文件夹中的 styles.xml 吗?例如 v21。
  • 制作没有操作栏的样式

标签: android android-listview material-design android-logcat android-toolbar


【解决方案1】:

尝试更改您的style.xml,如下所示它对我有用:

<resources>

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

    </style>

</resources>

根据开发人员指南,我们必须使用NoActionBar 主题才能将工具栏用作操作栏。 还有extends您的活动ActionBarActivity

编辑 使用AppCompatActivity 而不是ActionBarActivity,它已被弃用。

【讨论】:

  • 您应该扩展 AppCompatActivity。 ActionBarActivity 现在已被弃用。
【解决方案2】:

尝试执行以下操作:- 从您的toolbar.xml 中删除此行:-

android:id="@+id/toolbar"

并在你的 activitymain_xml 文件中添加这一行:-

android:id="@+id/toolbar"

并扩展父主题Theme.AppCompat.Light.NoActionBar

【讨论】:

  • 它对我没有帮助@Rajat
  • 更改后是相同的错误还是不同的错误?
【解决方案3】:

AppCompat 不支持当前的主题功能

看来,你的 Theme 风格不对。

您的style.xml 应包含以下几行:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

P.S.:您的主题应该扩展 Theme.AppCompat.Light.NoActionBar 而不是 Theme.AppCompat.Light

【讨论】:

  • 尝试使用Theme.AppCompat.Light.NoActionBar 而不是Theme.AppCompat.Light
猜你喜欢
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 2019-06-01
  • 1970-01-01
相关资源
最近更新 更多