51

我有一个FrameLayout内部CoordinatorLayout来膨胀我的碎片,其中包含RecyclerView. 问题是,我想在加载时显示一个进度条RecyclerView,但进度条总是在屏幕顶部Toolbar。我尝试了各种布局、设置gravitycenterInParent但都没有奏效。那么有什么方法可以实现这一点吗?

4

6 回答 6

70
    android:layout_gravity="center" 

工作得很好。并删除您的 FrameLayout,这是多余的。

于 2015-12-11T10:41:06.150 回答
10

经过一番研究,我未能找到一种方法来完成这项工作,所以最后我这样做了:

  1. 将进度条放在 CoordinatorLayout 中,使用android:layout_gravity="center".
  2. 在活动中,使用 2 种方法来显示/隐藏进度条:

    public void showProgress() {
          progressBar.setVisibility(View.VISIBLE);
    }
    
    public void hideProgress() {
          progressBar.setVisibility(View.INVISIBLE);
    }
    
  3. 在片段中,通过 获取上述活动的引用getActivity(),将其转换为实际活动并调用必要的方法。

    context = getActivity();
    ((MainActivity)context).showProgress();
    

编辑:这似乎已在支持库 23.1.1 中修复

于 2015-12-12T03:54:49.033 回答
2

我创建了我的视图,如:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:wheel="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/order_list_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/order_list_collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/padding_normal">

                <TextView
                    android:id="@+id/order_list_outstanding_amount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@android:id/icon"
                    android:layout_margin="@dimen/margin_normal"
                    android:layout_marginLeft="@dimen/margin_high"
                    android:layout_toRightOf="@android:id/icon"
                    android:text="@string/format_outstanding_amout"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/font_large" />
            </RelativeLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>


        <android.support.v7.widget.RecyclerView
            android:id="@+id/order_list_recycler_view"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </android.support.v7.widget.RecyclerView>

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_margin="16dp"
            android:src="@android:drawable/ic_input_add"
            app:layout_anchor="@id/order_list_recycler_view"
            app:layout_anchorGravity="bottom|right|end" />

        <include
            android:id="@+id/order_list_empty_view"
            layout="@layout/empty_view"></include>

    <com.pnikosis.materialishprogress.ProgressWheel
        android:id="@+id/progress_wheel"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_gravity="center"
        wheel:matProg_barColor="@color/colorAccent" />
</android.support.design.widget.CoordinatorLayout>

在此处输入图像描述

于 2015-08-13T07:22:45.600 回答
1

将 CoordinatorLayout 放在 RelativeLayout 中,并在此 RelativeLayout 中添加进度条。将进度条的 CenterInParent 设置为 true。根据需要使进度条消失/可见。

于 2015-12-11T11:05:23.097 回答
1

如果要将视图居中在另一个视图中,则需要向所有侧面添加约束:

    app:layout_constraintBottom_toBottomOf="@id"
    app:layout_constraintLeft_toLeftOf="@id"
    app:layout_constraintRight_toRightOf="@id"
    app:layout_constraintTop_toTopOf="@id"

对于下面的示例,我只是将 TextView 置于约束布局的中间。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:text="This is a centered View"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

结果:

协调器布局中的居中视图

于 2018-05-09T02:39:57.260 回答
0

在 RelativeLayout 中使用 FrameLayout 并将 CenterInParent 属性设置为进度条。

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/myFrameLyt"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>
于 2015-07-23T05:19:56.443 回答