android – 工具栏下面有水平ProgressBar

借助 Android Lollipop和AppCompat-v7中的新工具栏API,他们正在删除许多自动功能,以使Toolbar / ActionBar更加强大.其中一个是进度条.由于Toolbar只是一个ViewGroup,我假设添加一个ProgressBar很简单.但是,我似乎无法让它正常工作.

我做了以下(使用SmoothProgressBar库):

// I instantiate the toolbar and set it as the actionbar
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    // I create a ProgressBar and set the drawable to the SmoothProgressBar drawable
    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setIndeterminateDrawable(new SmoothProgressDrawable.Builder(this).color(Color.BLUE).interpolator
            (new DecelerateInterpolator()).sectionsCount(4).separatorLength(8).speed(2f).mirrorMode(true).build());
    // I add the progressbar to the view with what I thought were the proper LayoutParams.
    Toolbar.LayoutParams params = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,20);
    params.gravity = Gravity.BottOM;
    mToolbar.addView(progressBar,params);
    progressBar.setIndeterminate(true);

我认为这可行,因为我只是将一个ProgressBar添加到ViewGroup的底部.但是,它根本没有显示,正在删除标题.下面你可以看到前后.有谁知道如何解决这个问题?我的目标是在操作栏下面放置一个ProgressBar.

之前

解决方法

最简单的方法是直接在XML-Layout文件添加ProgressBar.

1.一次性解决方

使用RelativeLayout作为root并使用android:layout_below将ProgressBar和主要内容保留在工具栏下方.

<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.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        android:id="@+id/loadProgressBar"
        style="@style/LoadProgressBar"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/toolbar"
        android:indeterminate="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical">
        <!-- Your Content here -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content Text"/>

    </LinearLayout>

</RelativeLayout>

现在,您可以在Activitiy onCreate方法中访问工具栏和ProgressBar

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    progressBar = (SmoothProgressBar) findViewById(R.id.loadProgressBar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }
}

2.使用include的一般解决方

更通用的方法是将工具栏和ProgressBar放在单独的XML布局文件中,并将其包含在活动布局中.

toolbar.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        android:id="@+id/loadProgressBar"
        style="@style/LoadProgressBar"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/toolbar"
        android:indeterminate="true"/>

</merge>

activity_main.xml中

<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">

    <include
        layout="@layout/toolbar"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical">
        <!-- Your Content here -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content Text"/>

    </LinearLayout>

</RelativeLayout>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...