在CoordinatorLayout中是否可能将布局/视图居中?

51

我在CoordinatorLayout中放了一个FrameLayout,用来加载包含RecyclerView的片段。问题是,我想在加载RecyclerView时显示进度条,但进度条总是在Toolbar下面的屏幕顶部。我尝试了各种布局,设置了gravitycenterInParent,但都没有起作用。那么有什么方法可以实现这个功能吗?

6个回答

72
    android:layout_gravity="center" 

运行良好。另外,移除你的FrameLayout,它是多余的。


FrameLayout 是用于填充 Fragment 的视图,它不是最外层的 ViewGroup。 - Kise

11
在进行了一些研究后,我没能找到让这个工作的方法,所以最终我做了以下处理:
  1. 将进度条放置在带有 android:layout_gravity="center" 属性的 CoordinatorLayout 中。
  2. 在活动中,编写2个方法来显示/隐藏进度条:

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

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

编辑:这个问题似乎在支持库 23.1.1 中已经修复。


“fixed”具体指什么?是如何修复的? - android developer
@androiddeveloper 上次我检查了支持库23.1.1,我能够按照问题描述实现所需的结果,而不需要使用这个答案中的“hack”方法。 - Kise
哦,我明白了。只是在我的情况下,只有上部区域扩展时它才处于中心位置,而当它被默认折叠时,我希望它能够居中显示... - android developer
@androiddeveloper 也许可以使用 AppBarStateChangeListener 并在折叠时为 ProgressBar 设置 paddingTop - Kise
我根据里面的内容添加了边距(填充也可以),现在没问题了。谢谢。 - android developer

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>

enter image description here


如果您不使用片段,它可能会起作用。然而,正如我在第一篇帖子中所描述的那样,我在CoordinatorLayout内部使用FrameLayout来填充我的片段,其中包含一个RecyclerView、一个TextView和一个ProgressBar。在这种情况下,重力属性都不起作用。 - Kise

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>

结果:

centered view in coordinator layout


谢谢,但是这个问题已经不再适用于当前版本的支持库。 - Kise
8
明白,您的问题是关于 CoordinatorLayout 而不是 ConstraintLayout - Zach
1
@Zach 所以你只需要设置 layout_gravity = "center" - Dimas Mendes

1
将CoordinatorLayout放置在RelativeLayout中,并在此RelativeLayout中添加progressbar。将progressbar的CenterInParent属性设置为true。根据需要将progressbar设置为Gone/Visible。

实际上,您不需要将CoordinatorLayout放在RelativeLayout中,只是为了使进度条居中。 layout_gravity 就可以很好地完成这项工作。我的意思是进度条位于其他xml文件中,在这种情况下,将其置于中心是不可能的。 - Kise

0
使用FrameLayout嵌套RelativeLayout,并将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>

谢谢,但进度条必须在片段的XML中。 - Kise

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接