微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐