Android中SlidingDrawer开发报错You need to use a Theme.AppCompat theme (or descendant) with this activity.

Android抽屉开发报错You need to use a Theme.AppCompat theme (or descendant) with this activity.
方法1:
创建的activity时,如果不是那么强烈需要继承自AppCompatActivity,就直接继承Activity。
如将activity继承自AppCompatActivity:public class MainActivity extends ActionBarActivity 改成activity继承自Activity:
方法2:
还是想继承自AppCompatActivity,那么根据提示来使用AppCompat的theme,即将AndroidManifest.xml文件中关于Activity的主题配置改成:
android:theme=”@style/Theme.AppCompat.Light.NoActionBar”
完整代码如下:

<activity android:name=".module.view.activity.KuwoMusicPlayActivity"
            android:theme="@style/Theme.AppCompat.Light.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

这个方法可以解决问题。
android(SlidingDrawer)实现代码
布局文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="@android:color/holo_red_light"
    tools:context="com.json.zjs.a20180905.MainActivity">

    <SlidingDrawer
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:content="@+id/content"
        android:handle="@+id/layout1"
        android:orientation="horizontal">
        <!--设置引导按钮-->
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="35dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
        <!--设置抽屉内容-->
        <TextView
            android:id="@+id/content"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:background="#f0f0"
            android:text="asdcfasghda" />

    </SlidingDrawer>
</RelativeLayout>
activity文件

package com.json.zjs.a20180905;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

}

}
style 文件:

<resources>

    <!-- Base application theme. 设置去除actionbar之后全屏显示,标题栏去掉 -->
    <style name="AppTheme" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
清单文件:
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

如何实现去掉标题栏:
第一种:
style 中设置

<style name="AppTheme" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen">

activity直接集成Activity,清单文件中的activity的theme不做处理!
第二中:、
继承AppCompatActivity,但是需要在清单文件中指定theme
theme需要在style中手动设置

<style name="AppTheme.NoTitle_FullScreen"> <!--自定义主题名称-->
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

效果:这里写图片描述
打开抽屉
这里写图片描述
支持点击和拖拽
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值