0

我正在尝试在我的 android 应用程序中创建一个带有加载符号的新警报框。工作正常。下面是我的代码。

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.custom, null));
progress = new ProgressBar(this);
builder.setMessage("Message");
builder.setView(progress);
//builder.setCancelable(false);
builder.create().show();

我的 Custom.XML 看起来像

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff" >

<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="32dp"
             android:layout_height="32dp"
             android:layout_gravity="center"/>

</RelativeLayout>

我的 res/styles.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="android:Theme.Light">
        <!--
            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" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

    <style name="maxWid">
        <item name="android:textColor">#000000</item>

        <item name="android:layout_marginTop">50dp</item>
        <item name="android:layout_marginBottom">50dp</item>
        <item name="android:layout_marginLeft">10dp</item>
    </style>
    <style name="apBtn">
        <item name="android:textColor">#000</item>
        <item name="android:layout_alignParentLeft">true</item>
    </style>

</resources>

res/values-v11/styles.xml 是

<resources>

    <!--
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
        <!-- API 11 theme customizations can go here. -->
    </style>

</resources>

res/values-v14/styles.xml 是

<resources>

    <!--
        Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    </style>

</resources>

我的 AndroidManifest.cml

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

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

    <permission
        android:name="com.myapp.kmc.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.myapp.kmc.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.myapp.kmc.MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.myapp.kmc.LoginActivity"
            android:label="@string/title_activity_login"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>
        <activity
            android:name="com.myapp.kmc.ListActivity"
            android:label="@string/title_activity_list"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>
        <activity
            android:name="com.myapp.kmc.CompleteActivity"
            android:label="@string/title_activity_complete"
            android:theme="@android:style/Theme.NoTitleBar" >
        </activity>

        <receiver
            android:name="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.myapp.kmc" />
            </intent-filter>
        </receiver>

        <service android:name="com.myapp.kmc.GCMIntentService" />

        <activity
            android:name="com.myapp.kmc.RegisterDevice"
            android:theme="@android:style/Theme.NoTitleBar"
            android:label="@string/title_activity_register_device" >
        </activity>
    </application>

</manifest>

在 android 4.1.2 中测试

但是,当我创建一个新应用程序并遵循此代码时,我会收到一个像这样的警报框

在此处输入图像描述

但是当我在现有应用程序中实现相同的代码时。我收到这样的警报框

在此处输入图像描述

因为我想要现有应用程序中的第一个。它毁了我的时间..

谁能帮帮我吗。我是android新手提前谢谢

4

2 回答 2

1

您现有的应用程序使用Theme.Black,这就是对话框具有黑色背景的原因。更改主题以Theme.Holo.Light修复它。

有关执行此操作的推荐方法,请查看我的其他答案


编辑:看起来您正在将正确的主题应用于您的<application>标签,但随后您通过为每个活动设置Theme.NoTitleBar(它是 的后代)来覆盖它。Theme.Black

从标签中删除android:theme属性,<activity>它将起作用。如果要删除标题和操作栏,请将windowNoTitlewindowActionBar属性应用于主题定义:

<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
于 2013-07-28T05:45:17.947 回答
0

如果您想使用 Holo 主题,您必须将 API 级别设置为 14(默认启用硬件加速);

尝试插入:

<uses-sdk android:minSdkVersion="14" />

在您的 AndroidManifest.xml 中。

于 2013-08-23T21:06:28.360 回答