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

Android 在ActionBar上设置不确定进度条的样式,android,android-actionbar,android-progressbar,Android,Android Actionbar,Android Progressbar,我想在动作栏上放一个进度条,但是设置 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setProgressBarIndeterminateVisibility(true); 在onCreate方法上,我想会生成一个中等大小的progressBar(48度x 48度)。我想更改progressBar的大小,但找不到如何更改。您能帮我吗?您需要创建自己的样式以应用于ActionBar。这方面有一个很好的教程,要设置中间

我想在动作栏上放一个进度条,但是设置

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

onCreate
方法上,我想会生成一个中等大小的progressBar(48度x 48度)。我想更改progressBar的大小,但找不到如何更改。您能帮我吗?

您需要创建自己的样式以应用于ActionBar。这方面有一个很好的教程,要设置中间进度条的样式,请尝试使用
indendenceprogressstyle
Widget.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>

@风格/动作酒吧
@牵引式/集成电路发射器
@风格/不确定性进展
@颜色/白色

对于ActionBarSherlock,您可以通过为Widget.ProgressBar.Small设置子项来更改不确定的进度,这在没有Sherlock的情况下也应该可以工作

我从SDK中的android-15 res文件夹中获取了drawables,一定要抓取progress\u small\u 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>

@style/Widget.Styled.ActionBar
@style/Widget.Styled.ActionBar
@风格/不确定性进展
@风格/不确定性进展
@可绘制/进度\小型\全息图
ActionBar上已设置样式的动画不确定进度条图标: 1.定义res/values/styles.xml(此示例使用Sherlock库在旧Android版本中提供actionbar,因此,如果您不使用Sherlock,则应使用经过批准的父级来定义自定义样式):


@style/Widget.Styled.ActionBar
@style/Widget.Styled.ActionBar
@style/不确定进度
@风格/不确定性进展
@可绘图/进度\不确定\自定义
2.定义res/drawable/progress\u undeterminate\u 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_进度_logo1和ic_进度_logo2)作为自定义进度不确定图标。通过添加新的项目元素,可以使用任意数量的图像/绘图。此外,还可以应用不同的效果/动画。更多信息请点击此处:

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

要使ActionBar上的进度条变小,您要做的是像这样覆盖
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>

...
@style/ActionBar.MyStyle
...
...
@style/ActionBarProgressBar.MyStyle
...
28dp
28dp
28dp
28dp

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

所有这些解决方案都让我很为难,因为我没有使用
actionbarsherlock
。我利用了弗拉斯托·本尼·拉瓦的答案,但一开始对我不起作用

以下是我一步一步做的:

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>
由于您只需缩小现有进度png的大小,因此无需导出任何图像并将其移动到可绘制位置等

这是我的样子,图标的大小非常适合我


在Create方法的
中粘贴下面的代码,并在中间模式下运行下面的栏。
如果要显示进度百分比,则注释
progressBar.setUndeterminate(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);
        }
    });

我必须与旧版本的Android SDK兼容,因此我必须保持它的简单和使用:

Styles.xml

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

对于需要调整的活动。

我编辑了我的回答。现在可以了。对不起,我以前没试过。如果要编辑较小的进度条,请改用“Widget.ProgressBar.Small”:即使现在我与ActionBarSherlock存在兼容性问题,它也可以工作。在ActionBarSherlock中,有一个属性@style/indendenceProgress。我现在很高兴:)谢谢如果你想有一个大小和填充合适的进度条,请在这里检查我的解决方案:它对我有效,但我必须使ActionBarIPS的父级=“@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse”删除android:background项,使其能够与我的dark ActionBar配合使用。效果很好,但它当然不会移动。如何在其上应用progress\u small\u white动画?单击操作项时,可以将其替换为实际的进度条布局:refreshItem.setActionView(MyProgressBarFrameLaOutingFlated);您可能希望提供一个完整的解决方案,而不仅仅是一个链接。谢谢
 <style name="TallerTitleBarDialogThemeStyle" parent="@android:style/Theme.Dialog">
    <item name="android:windowTitleSize">36dp</item>
</style>
android:theme="@style/TallerTitleBarDialogThemeStyle"