ActionBar上不确定进度条的样式设置

18

我想在操作栏上放置一个进度条,但设置方法我不太清楚。

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);  
setProgressBarIndeterminateVisibility(true);

onCreate方法中生成了一个中等大小的进度条(48dip x 48dip),我想改变进度条的大小,但是我找不到如何做到。你能帮我吗?

7个回答

27
你需要创建自己的样式来应用于ActionBar。在Android Developers Blog上有一篇很棒的教程。要为中间进度条设置样式,请尝试使用indeterminateProgressStyleWidget.ProgressBar.Small。这是一个快速的示例。
<style name="YourTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/ActionBarIPS</item>
</style>

<style name="IndeterminateProgress" parent="@android:style/Widget.ProgressBar">
    <item name="android:indeterminateDrawable">@drawable/ic_launcher</item>
</style>

<style name="ActionBarIPS" parent="@android:style/Widget.ActionBar">
    <item name="android:indeterminateProgressStyle">@style/IndeterminateProgress</item>
    <item name="android:background">@color/white</item>
</style>

我编辑了我的回复。现在它可以工作了。对不起,我之前没有测试过它。如果你想编辑较小的进度条,请使用"Widget.ProgressBar.Small"。 :) - adneal
3
在ActionBarSherlock中,有这个属性<item name="abIndeterminateProgressStyle">@style/IndeterminateProgress</item>。我现在非常开心 :) 谢谢 - Matroska
如果您想要一个大小合适且有填充的进度条,请查看我的解决方案:https://dev59.com/SWUp5IYBdhLWcg3wGEkR - petrnohejl
1
它对我起作用了,但我不得不将ActionBarIPS的parent属性设置为"@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse",并删除android:background项,以便它能够与我的深色ActionBar一起使用。 - user1376112
如何修改进度条的方向,使其变为水平方向,就像一些下拉刷新库(例如:https://github.com/chrisbanes/ActionBar-PullToRefresh)中所展示的那样? - android developer
显示剩余2条评论

15

在ActionBar上使用样式化的、带动画效果的不确定进度条图标:

1.定义res/values/styles.xml(本示例使用Sherlock库来在旧版本的Android中提供ActionBar,因此如果您没有使用Sherlock,则应使用适当的父级来定义您的自定义样式):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyTheme" parent="Theme.Sherlock">
        <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
        <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
    </style>

    <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar">
        <item name="android:indeterminateProgressStyle">@style/IndeterminateProgress</item> 
        <item name="indeterminateProgressStyle">@style/IndeterminateProgress</item> 
    </style>

    <style name="IndeterminateProgress" parent="@android:style/Widget.ProgressBar.Small"> 
       <item name="android:indeterminateDrawable">@drawable/progress_indeterminate_custom</item> 
    </style> 
</resource>

2. 定义res/drawable/progress_indeterminate_custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <rotate
        android:drawable="@drawable/ic_progress_logo1"
        android:fillAfter="true"
        android:fromDegrees="-180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="180" />
    </item>
     <item>
    <rotate
        android:drawable="@drawable/ic_progress_logo2"
        android:fillAfter="true"
        android:fromDegrees="180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="-180" />
</item>
</layer-list>

3.以上代码使用了2张图像(ic_progress_logo1和ic_progress_logo2)作为自定义进度不确定图标。您可以添加新的项目元素来使用尽可能多的图像/可绘制对象。此外,您还可以应用不同的效果/动画。更多信息请参见:Animation Resources

4.使用AndroidManifest.xml中创建的样式:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:name=".application.MyApplication">
    <activity
        android:name=".activities.MyActivity"
        android:label="@string/app_name"
        android:theme="@style/MyTheme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

5. 享受您的动画自定义不定进度图标 :D


你可能想提供一个完整的解决方案,而不仅仅是一个链接。谢谢。 - Valentin Despa

8

我在处理这些解决方案时遇到了困难,因为我没有使用actionbarsherlock。我借鉴了Vlasto Benny Lava的答案,但最初并没有起作用。

以下是我一步步所做的:

1) 打开 styles.xml 文件,它应该在 /res/values/styles.xml 下。如果不存在,请创建它。

2) 粘贴以下内容:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="myTheme" parent="android:Theme.Holo">
        <item name="android:actionBarStyle">@style/ActionBar.MyStyle</item>
    </style>

    <style name="ActionBar.MyStyle" parent="@android:style/Widget.Holo.ActionBar">
        <item name="android:indeterminateProgressStyle">@style/ActionBarProgressBar.MyStyle</item>
    </style>

    <style name="ActionBarProgressBar.MyStyle" parent="@android:style/Widget.Holo.ProgressBar.Small">
        <item name="android:minWidth">28dp</item>
        <item name="android:maxWidth">28dp</item>
        <item name="android:minHeight">28dp</item>
        <item name="android:maxHeight">28dp</item>
    </style> 
</resources>

我遇到的一个问题是,您不能使用主题名称“AppTheme”,否则覆盖将不起作用。我使用了“MyTheme”。该主题使用Holo,因此在下面的图像中可以看到黑色的ActionBar。如果您希望拥有白色的操作栏,则可以使用android:Theme.Holo.Light替换android:Theme.Holo

3) 编辑AndroidManifest.xml

您的<application>必须包含android:theme="@style/myTheme",以便应用程序使用您在styles.xml中覆盖的样式。

例如,我的清单应用程序标签文件如下:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/myTheme"
    android:name="Application Name Here" >

因为只需要缩小现有进度PNG文件的大小,所以无需导出任何图像并将其移动到可绘制位置等操作。下面是我的示例,图标的大小非常适合我。 enter image description here

2
为了使ActionBar上的ProgressBar更小,你需要像这样重写Widget.Holo.ProgressBar.Small。
<style name="Theme.MyStyle" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    ...
    <item name="actionBarStyle">@style/ActionBar.MyStyle</item>
    ...
</style>

<style name="ActionBar.MyStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
    ...
    <item name="android:indeterminateProgressStyle">@style/ActionBarProgressBar.MyStyle</item>
    ...
</style>

<style name="ActionBarProgressBar.MyStyle" parent="@android:style/Widget.Holo.ProgressBar.Small">
    <item name="android:minWidth">28dp</item>
    <item name="android:maxWidth">28dp</item>
    <item name="android:minHeight">28dp</item>
    <item name="android:maxHeight">28dp</item>
</style>

并将大小设置为您想要的任何大小。


2

对于ActionBarSherlock,您可以通过将Widget.ProgressBar.Small设置为父级来更改不确定进度,这也适用于没有Sherlock的情况。

我从SDK中的android-15资源文件夹中获取了可绘制对象,请确保获取progress_small_white.xml和相关资源。

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <style name="Dark" parent="Theme.Sherlock">
         <item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
         <item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
     </style>

     <style name="Widget.Styled.ActionBar" parent="Widget.Sherlock.Light.ActionBar">
         <item name="android:indeterminateProgressStyle">@style/IndeterminateProgress</item> 
         <item name="indeterminateProgressStyle">@style/IndeterminateProgress</item> 
     </style>

      <style name="IndeterminateProgress" parent="@android:style/Widget.ProgressBar.Small"> 
        <item name="android:indeterminateDrawable">@drawable/progress_small_holo</item> 
    </style> 
 </resource>

工作得很好,但它当然没有移动。如何在上面应用progress_small_white动画? - urSus
当您单击操作项时,可以将其替换为实际的进度条布局:<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center"> <ProgressBar android:layout_width="16dp" android:layout_height="16dp" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_gravity="center" style="?android:attr/indeterminateProgressStyle"/> </FrameLayout>refreshItem.setActionView(myProgressBarFrameLaoutInflated); - ThanksMister

1
将下面的代码粘贴到您的onCreate方法中,并在中间模式下运行以下栏。
如果要显示进度百分比,请注释progressBar.setIndeterminate(true);方法并取消注释progressBar.setProgress(65)方法并维护您的进度。
       progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
       progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 24));
     //progressBar.setProgress(65);
     //progressBar.setBackgroundDrawable(getResources().getDrawable(R.color.lock_background_greeen));
      progressBar.setIndeterminate(true);

    // retrieve the top view of our application
      final FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
      decorView.addView(progressBar);


      ViewTreeObserver observer = progressBar.getViewTreeObserver();
      observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            View contentView = decorView.findViewById(android.R.id.content);
            progressBar.setY(contentView.getY() - 10);

            ViewTreeObserver observer = progressBar.getViewTreeObserver();
            observer.removeGlobalOnLayoutListener(this);
        }
    });

0

我必须兼容旧版本的Android SDK,所以我必须保持简单并使用:

Styles.xml

 <style name="TallerTitleBarDialogThemeStyle" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleSize">36dp</item>
</style>

AndroidManifest.xml

android:theme="@style/TallerTitleBarDialogThemeStyle"

对于这个活动,我需要进行调整。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接