如何在应用程序范围内使用Android的AppCompat?

时间:2015-03-03 17:36:38

标签: android android-theme android-appcompat android-styles android-compatibility

IDE :Android Studio 1.1.0

ENV :使用Android API v10在USB连接手机上进行调试。


我正在尝试使用AppCompat来支持Android API v8及更高版本。绘制活动时编译很好但崩溃,说:

lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

...但是,在AndroidManifest.xml中我有:

    <application
        android:name=".objects.PikCMS"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/ThemeOverlay.AppCompat"
        >

谢谢你&amp;请原谅我的沮丧。我一直在阅读很多相关但无益的信息。 } 8,(


其他信息

  • 我不想强制使用特定的主题。
  • 我想避免为不同的API版本使用不同的文件。但是,我不介意将相同属性的不同版本彼此相邻。
  • 我在style.xml中为v11 中引入的项目(例如“buttonBarStyle”&amp;“showDividers”)中出错。在阅读了这个问题之后,我原以为AppCompat会为pre11 API“填空”但是(在我的配置中)它不是。目前这些都被注释掉了 - 直到我找到如何在AppCompat中使用它们。同样,我遇到了布局文件的问题 - 这些有问题的属性被删除了。
  • 活动类:Login.java extends:ActionBarActivity:。我也导入“ android.support.v7.app.ActionBarActivity ”,但我不确定是否需要全部,因为我没有隐含地使用操作栏(但我可能会在未来的)。

<小时/> <小时/>

代码文件

build.gradle(Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        applicationId "com.kjackson324.pikcms"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile fileTree(include: ['*.jar'], dir: 'libs')
}


<小时/>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kjackson324.pikcms" >

    <application
        android:name=".objects.PikCMS"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/ThemeOverlay.AppCompat"
        >
        <uses-permission android:name="android.permission.BLUETOOTH" />

        <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" />

        <activity
            android:name=".Login"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_login"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.Login" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Navigation"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_navigation"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.Navigation" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PickHistory"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/title_activity_pik_log"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.PickHistory" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <!--

            ...bla, bla, bla...

            (much more of the same, no new attributes used)
        -->

        <activity
            android:name=".ConfigLocFields"
            android:label="@string/title_activity_config_loc_fields"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.kjackson324.pikcms.Navigation" />
            <intent-filter>
                <action android:name="com.kjackson324.pikcms.ConfigLocFields" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>


<小时/>

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="simple_center" parent="">
        <!--item name="android:layout_gravity">center</item-->
        <!--item name="android:gravity">center</item-->
        <item name="android:dialogLayout">?android:layout_gravity</item>
    </style>

    <style name="lblLoggedIn" parent="@android:style/TextAppearance.Small">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:text">@string/sys_logged_in</item>
        <item name="android:layout_gravity">top|right</item>
        <item name="android:gravity">top|center_vertical</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:paddingRight">4dp</item>
    </style>

    <style name="tvLoggedIn" parent="@android:style/TextAppearance.Small">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:inputType">none</item>
        <item name="android:gravity">top</item>
        <item name="android:layout_gravity">top|left</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textColor">@android:color/white</item>
        <item name="android:layout_weight">2</item>
    </style>

    <style name="layout_master">
        <item name="android:orientation">vertical</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <!--item name="showDividers">end</item-->
        <item name="android:layout_alignParentTop">true</item>
        <item name="android:layout_centerHorizontal">true</item>
    </style>

    <style name="layout_loggedin">
        <!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">?android:attr/dividerVertical</item>
        <item name="android:gravity">bottom|center</item>
    </style>

    <style name="layout_bodyscroll">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:fillViewport">true</item>
        <item name="android:layout_height">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:layout_marginTop">@dimen/activity_bodyscroll_margin</item>
        <item name="android:layout_marginBottom">@dimen/activity_bodyscroll_margin</item>
        <item name="android:layout_marginLeft">@dimen/activity_bodyscroll_margin</item>
        <item name="android:layout_marginRight">@dimen/activity_bodyscroll_margin</item>
    </style>

    <style name="layout_bodylinear">
        <item name="android:orientation">vertical</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="layout_btnbar_frame">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:layout_weight">1</item>
    </style>

    <style name="layout_btnbar_linear" parent="">
        <!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:orientation">horizontal</item>
        <item name="android:background">?android:attr/dividerVertical</item>
        <item name="android:gravity">bottom|center</item>
        <item name="android:layout_gravity">bottom</item>
    </style>

    <style name="layout_btnbar_btn" parent="">
        <!--item name="buttonBarStyle">?android:attr/buttonBarStyle</item-->
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:textColor">@android:color/primary_text_dark</item>
        <item name="android:paddingTop">@dimen/activity_vertical_margin</item>
        <item name="android:paddingBottom">@dimen/activity_vertical_margin</item>
    </style>

    <style   name="menu_btn"    parent="layout_btnbar_btn">
        <item name="android:textAppearance">@android:style/TextAppearance.Large</item>
        <item name="android:textStyle">bold</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

    <style name="hidden">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">0dp</item>
        <item name="android:editable">false</item>
        <item name="android:autoText">false</item>
        <item name="android:visibility">invisible</item>
        <item name="android:inputType">none</item>
    </style>

    <style name="field_edittext">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginBottom">7dp</item>
        <item name="android:layout_marginTop">3dp</item>
        <item name="android:layout_marginLeft">2dp</item>
        <item name="android:layout_marginRight">2dp</item>
        <item name="android:layout_column">1</item>
        <item name="android:layout_weight">2</item>
        <item name="android:textColor">@android:color/primary_text_dark</item>
        <item name="android:autoText">true</item>
        <item name="android:gravity">bottom</item>
    </style>

    <style name="tvFieldLabel">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
        <item name="android:layout_gravity">top</item>
        <item name="android:gravity">top</item>
    </style>

    <style name="field_edittext_note" parent="field_edittext">
        <item name="android:layout_height">125dp</item>
        <item name="android:gravity">top</item>
    </style>

    <style name="tvTableField">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_gravity">bottom|right</item>
        <item name="android:layout_weight">0</item>
        <item name="android:layout_column">0</item>
        <item name="android:textAppearance">@android:style/TextAppearance.Medium</item>
        <item name="android:textColor">@android:color/secondary_text_dark</item>
        <item name="android:gravity">center_vertical</item>
        <item name="android:inputType">none</item>
    </style>

    <style name="spinTails">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <!--item name="android:background">?android:attr/dividerVertical</item-->
        <item name="android:padding">8dp</item>
    </style>


    <style name="tvFieldColPrimary" parent="tvTableField">
        <item name="android:textColor">@android:color/primary_text_dark</item>
        <item name="android:paddingRight">8dp</item>
    </style>

    <style name="tvFieldColSecondary" parent="tvFieldColPrimary">
        <item name="android:textColor">@android:color/secondary_text_dark</item>
    </style>

</resources>


<小时/>

./ layout / activity_login.xml (我的第一个活动)

<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"
    tools:context="com.kjackson324.pikcms.Login"
    android:id="@+id/layLogin" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/tvId"
            android:visibility="invisible"/>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:id="@+id/scrollView4"
            android:layout_weight="1">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/linearLayout13"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/entry_employee"
                    android:id="@+id/tvName"
                    />

                <Spinner
                    android:id="@+id/spinEmployees"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:layout_gravity="center_horizontal"
                    android:clickable="true"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="@string/lbl_password"
                    android:id="@+id/tvPassword"
                    android:layout_marginTop="@dimen/activity_vertical_margin"/>

                <EditText
                    android:id="@+id/txtPassword"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:ems="10"
                    android:text="admin"/>

            </LinearLayout>
        </ScrollView>

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/linearLayout14"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="bottom|center"
                android:weightSum="2"
                android:layout_gravity="bottom">

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/btn_begin"
                    android:id="@+id/btnLogin"
                    android:background="@android:color/transparent"
                    android:layout_weight="1"
                    android:textColor="@android:color/primary_text_dark"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    android:onClick="onClick"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/btn_clear"
                    android:id="@+id/button7"
                    android:background="@android:color/transparent"
                    android:layout_weight="1"
                    android:textColor="@android:color/primary_text_dark"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"/>
            </LinearLayout>
        </FrameLayout>

    </LinearLayout>

</RelativeLayout>


<小时/>

设备| logcat的

03-03 11:14:37.386    1891-1891/com.kjackson324.pikcms D/AndroidRuntime﹕ Shutting down VM
03-03 11:14:37.386    1891-1891/com.kjackson324.pikcms W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40018560)
03-03 11:14:37.413    1891-1891/com.kjackson324.pikcms E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kjackson324.pikcms/com.kjackson324.pikcms.Login}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1696)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
            at android.app.ActivityThread.access$1500(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3806)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
            at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
            at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
            at com.kjackson324.pikcms.Login.onCreate(Login.java:38)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1660)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
            at android.app.ActivityThread.access$1500(ActivityThread.java:124)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3806)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

您需要在清单的应用标签中替换您的主题:

<application android:theme="@style/Theme.AppCompat">

ThemeOverlay只能与带有新actionBarTheme属性的actionBar一起使用或与工具栏一起使用。更多信息:https://chris.banes.me/2014/11/12/theme-vs-style/