android – 工具栏下面有水平ProgressBar

android – 工具栏下面有水平ProgressBar,第1张

概述借助 Android Lollipop和AppCompat-v7中的新工具栏API,他们正在删除许多自动功能,以使Toolbar / ActionBar更加强大.其中一个是进度条.由于Toolbar只是一个ViewGroup,我假设添加一个ProgressBar很简单.但是,我似乎无法让它正常工作. 我做了以下(使用SmoothProgressBar库): // I instantiate the 借助 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"                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

@OverrIDeprotected 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"                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 – 工具栏下面有水平ProgressBar全部内容,希望文章能够帮你解决android – 工具栏下面有水平ProgressBar所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1130445.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存