ActionBar下的ProgressBar

zub*_*rto 78 android android-progressbar android-actionbar

问题摘要:如何在Chrome应用程序中进行ProgressBar集成ActionBar

详细信息:请查看Chrome的此屏幕截图:

Chrome for Android截图

我想像这样创建一个Action Bar.在Action Bar的下方,有一个ProgressBar根据页面加载填充.我从很多应用程序中看过这个例子,比如Feedly,但是我无法创建自己的实现.我尝试使用Android自己的API来创建它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Request Permission to display the Progress Bar...
    this.requestWindowFeature(Window.FEATURE_PROGRESS);
            this.setWindowContentView(R.layout.activity_main)
    super.onCreate(savedInstanceState);

    this.setProgressBarIndeterminate(true);
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码只会导致ProgressBar显示 Action Bar上,如下所示:

我的应用截图

那么,如何让我的ProgressBar出现在Action Bar下,就像Chrome应用程序一样?

Ant*_*nit 56

我对上面接受的答案并不完全满意,所以我自己做了一些额外的研究.

我相信他们使用的技巧是他们在视图层次结构中检索了顶视图并在其中DecorView添加了进度条.这样,进度条会在操作栏和内容区域上显示.请注意,SD的答案将进度条放入内容区域,并从实际内容中"窃取"空间,这可能会导致意外结果.

此实现的示例截图:

进度条

ProgressBarIndeterminate

只需将此代码放入onCreate某些活动的方法中,它应该工作:

// create new ProgressBar and style it
final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 24));
progressBar.setProgress(65);

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

// Here we try to position the ProgressBar to the correct position by looking
// at the position where content area starts. But during creating time, sizes 
// of the components are not set yet, so we have to wait until the components
// has been laid out
// Also note that doing progressBar.setY(136) will not work, because of different
// screen densities and different sizes of actionBar
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.removeOnGlobalLayoutListener(this);
    }
});
Run Code Online (Sandbox Code Playgroud)

您可以使用LayoutParams的height参数来设置progressBar更宽或更窄,但您可能必须调整-10偏移量.

造型

不幸的是,你可以看到进度条的丑陋灰色背景.要删除它,只需按id搜索背景并尝试隐藏它就行不通.要删除背景,我必须创建系统版本的相同drawble并删除背景项.

TL; DR:创建文件progress_horizontal_holo_no_background_light.xml并粘贴此drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%"
               android:drawable="@drawable/progress_secondary_holo_light" />
    </item>

    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%"
               android:drawable="@drawable/progress_primary_holo_light" />
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

将适当的.png drawables复制sdk/platforms/android-xx/data/res/drawable-xxx/到您的项目中,然后在您可以添加的代码中:

progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress_horizontal_holo_no_background_light));
Run Code Online (Sandbox Code Playgroud)

额外:不确定进度条

Pre-KitKat版本的不确定进度条非常难看且有些滞后.你可以下载新的smooth progressBar调用ButteryProgressBar.只需在谷歌上搜索它(我不能发布任何更多的链接,因为我是新来的:[),将类添加到您的项目中,您可以简单地用此代码替换以前的ProgressBar并具有脆弱的不确定进度条:

final ButteryProgressBar progressBar = new ButteryProgressBar(this);
progressBar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 24));
Run Code Online (Sandbox Code Playgroud)

您可能还需要简化此代码:

final TypedArray ta = c.obtainStyledAttributes(attrs, R.styleable.ButteryProgressBar);
try {
    mBarColor = ta.getColor(R.styleable.ButteryProgressBar_barColor,
            c.getResources().getColor(android.R.color.holo_blue_light));
    mSolidBarHeight = ta.getDimensionPixelSize(
            R.styleable.ButteryProgressBar_barHeight,
            Math.round(DEFAULT_BAR_HEIGHT_DP * mDensity));
    mSolidBarDetentWidth = ta.getDimensionPixelSize(
            R.styleable.ButteryProgressBar_detentWidth,
            Math.round(DEFAULT_DETENT_WIDTH_DP * mDensity));
} finally {
    ta.recycle();
}
Run Code Online (Sandbox Code Playgroud)

这段代码:

mBarColor = c.getResources().getColor(android.R.color.holo_blue_light);
mSolidBarHeight = Math.round(DEFAULT_BAR_HEIGHT_DP * mDensity);
mSolidBarDetentWidth = Math.round(DEFAULT_DETENT_WIDTH_DP * mDensity);
Run Code Online (Sandbox Code Playgroud)

希望我帮助:)

  • @GerritHoekstra:如果`getSupportActionBar().getHeight()`函数有效,你几乎就在那里.`progressBar.setY()`函数设置相对于屏幕顶部的位置,包括状态栏.所以你需要做的是`setY(supportActionBarHeight + Math.ceil(25*context.getResources().getDisplayMetrics().density))`.状态栏是25px高(mdpi)并且具有更高的密度(hdpi/xhdpi/...).请尝试并告诉我它是否有效. (2认同)

S.D*_*.D. 25

从下面的类扩展活动,在每个类的顶部(ActionBar下方)都有一个ProgressBar,以及一个getProgressBar()方法:

家长班:

public abstract class ProgressActivity extends Activity {
    private ProgressBar mProgressBar;

    @Override
    public void setContentView(View view) {
        init().addView(view);
    }

    @Override
    public void setContentView(int layoutResID) {
        getLayoutInflater().inflate(layoutResID,init(),true);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        init().addView(view,params);
    }

    private ViewGroup init(){
        super.setContentView(R.layout.progress);
        mProgressBar = (ProgressBar) findViewById(R.id.activity_bar);
        return (ViewGroup) findViewById(R.id.activity_frame);
    }

    protected ProgressBar getProgressBar(){
        return mProgressBar;
    }
}
Run Code Online (Sandbox Code Playgroud)

布局(progress.xml):

<?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">
<ProgressBar android:id="@+id/activity_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="-8dp"
    style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
    />
  <FrameLayout android:id="@+id/activity_frame"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)


Ale*_*uti 7

现在这是使用SwipeRefreshLayout可以获得的本机行为.

您可以使用a包装可滚动视图,SwipeRefreshLayout然后您只需要监听onRefresh事件:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
    swipeLayout.setOnRefreshListener(this);
    swipeLayout.setColorScheme(android.R.color.holo_blue_bright, 
            android.R.color.holo_green_light, 
            android.R.color.holo_orange_light, 
            android.R.color.holo_red_light);
}


@Override public void onRefresh() {
    new Handler().postDelayed(new Runnable() {
        @Override public void run() {
            swipeLayout.setRefreshing(false);
        }
    }, 5000);
}
Run Code Online (Sandbox Code Playgroud)

这个博客中可以找到一个简单的教程.