Android Studio 0.4.5

用于创建自定义对话框的Android文档:http://developer.android.com/guide/topics/ui/dialogs.html

如果您想要一个自定义对话框,您可以将活动显示为对话框,而不是使用对话框api。简单地创建一个活动,并将其主题设置为theme . holo . dialog in <activity> manifest元素:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

然而,当我尝试这样做时,我得到以下异常:

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

我支持以下,我不能使用大于10的最小值:

minSdkVersion 10
targetSdkVersion 19

在我的风格中,我有以下几点:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

在我的清单上,我有这样的活动:

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:theme="@android:style/Theme.Holo.Light.Dialog"
            android:name="com.ssd.register.Dialog_update"
            android:label="@string/title_activity_dialog_update" >
        </activity>

创建这样的对话框是我一直在做的事情,因为我已经完成了布局。

谁能告诉我如何解决这个问题?


当前回答

这真的迫使我张贴我自己的答案。

因为我正在使用Theme.AppCompat.Light。NoActionBar,也用支持兼容性的导入替换了所有AlertDialog实例,但在v21 (Lollipop)下仍然面临问题。

我不喜欢改变主题的想法,这是合适的。 所以在2天之后,我终于想到了其他为AndroidManifest.xml指定AppTheme的库。

我发现还有另外一个:Paytm Checkout SDK。

因此,下面的更改解决了这个问题。

Renaming AppTheme using for my app to 'XYZAppTheme' using tools:replace method in AndroidManifest of my project(app). <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".XYZ" android:allowBackup="true" android:icon="@mipmap/ic_launcher_v2" android:label="@string/app_name" android:largeHeap="true" android:roundIcon="@mipmap/ic_launcher_v2_round" android:supportsRtl="true" android:theme="@style/XYZAppTheme" tools:replace="android:icon,android:theme"> <activity android:name=".xyz.SplashActivity" android:launchMode="singleTask" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

其他回答

更改所需活动的主题。这对我来说很管用:

<activity
            android:name="HomeActivity"
            android:screenOrientation="landscape"
            android:theme="@style/Theme.AppCompat.Light"
            android:windowSoftInputMode="stateHidden" />

改变主题从突出显示标签在图片。

只做

new AlertDialog.Builder(this)

而不是

new AlertDialog.Builder(getApplicationContext())

对我来说是使用ContextThemeWrapper的解决方案:

private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}

从Android -如何创建FAB编程?

确保在创建新的Alert Dialog时使用的是活动上下文,而不是应用程序或基本上下文。