【问题标题】:How can I make a custom progress bar in the action bar?如何在操作栏中制作自定义进度条?
【发布时间】:2011-07-28 10:16:58
【问题描述】:

与其说是操作栏,不如说是创建一个我需要帮助的自定义进度条。我想创建一个完全像 Catch Notes - https://ssl.gstatic.com/android/market/com.threebanana.notes/ss-480-0-9 https://market.android.com/details?id=com.threebanana.notes

我以多种不同的方式尝试了多次。我查看了许多教程,包括 Android 开发指南。在这一点上,我非常生气。请问有人可以帮帮我吗?

现在,我的操作栏正在调用通用进度条布局,效果很好。

switch (item.getItemId()) {
    case R.id.refresh:
        item.setActionView(R.layout.progress);
        return true;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">

<ProgressBar android:id="@+id/progress" 
style="?android:attr/progressBarStyleSmall"  
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
</ProgressBar>

</LinearLayout>

【问题讨论】:

标签: java android progress-bar


【解决方案1】:

要显示进度条,需要在 ActionBar.displayOptions 中开启 ActionBar.DISPLAY_SHOW_CUSTOM。

Catch notes 正在为其进度条使用自定义动画。我认为默认的 Holo(正常大小)是一个合理的替代方案,但要获得类似于他们特定的东西,您需要创建一个 animationDrawable。

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html

然后使用 getCustomView() 从 ActionBar 中获取自定义视图,找到您的 ImageView,并启动它的动画。

【讨论】:

    【解决方案2】:

    进度条周围的LinearLayout是不必要的;另外,请注意我在设置项目的操作视图后添加了“expandActionView()”。

    switch (item.getItemId()) {
        case R.id.refresh:
            item.setActionView(R.layout.progress);
            item.expandActionView();
            return true;
    

    在 res/layout/progress.xml 中有这个:

    <?xml version="1.0" encoding="utf-8"?>
    <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/progressBarStyleSmall"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
    </ProgressBar>
    

    从技术上讲,没有必要为此进行布局:

    switch (item.getItemId()) {
        case R.id.refresh:
            item.setActionView(new ProgressBar(this, null, android.R.attr.progressBarStyleSmall));
            item.expandActionView();
            return true;
    

    【讨论】:

    • 好吧,当我尝试使用上面的代码时,ProgressBar 出现在ActionBar 的左侧,并且列表导航微调器变得不可见。您是否知道如何将此ProgressBarMenuItem 放在同一位置?
    • @jaibatrik 我认为这完全取决于菜单项的“显示为操作”属性,一些项目被推开以便为进度条腾出空间(但这可以避免)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多