Android不确定进度对话框进度条样式

时间:2016-04-15 10:58:00

标签: android android-theme

我无法找到Android 22和23之间不确定ProgressDialog中显示的进度条样式的原因。

让我们采取一个非常简单的布局:

<?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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="example.com.progressbarcolor.MainActivity">

    <Button
        android:id="@+id/show_progress"
        android:text="Show progres"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true"
        android:indeterminate="true"
        android:visibility="visible"/>
</RelativeLayout>

按下按钮显示ProgressDialog:

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();

该应用的风格是:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

最后是gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "example.com.progressbarcolor"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

正如您在下面的图片中看到的那样,两个应用程序在布局中直接使用时显示相同的进度条,但ProgressDialog内部的颜色不同。

在Android 22上,进度对话框显示绿色进度条而不是红色进度条。

Nexus 5,API 22

Nexus 5 - API 22

Nexus 5X,API 23

Nexus 5X - API 23

1 个答案:

答案 0 :(得分:5)

Add this in Your Style :

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <!-- Used for the buttons -->
        <item name="colorAccent">@color/colorAccent</item>
        <!-- Used for the title and text -->
        <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
        <!-- Used for the background -->
        <item name="android:background">@android:color/transparent</item>
    </style>

and set to the dialog :

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this, R.style.MyAlertDialogStyle);