【问题标题】:Is it possible to center a layout/view inside CoordinatorLayout?是否可以在 CoordinatorLayout 中居中布局/视图?
【发布时间】:2015-10-13 05:10:54
【问题描述】:

我在CoordinatorLayout 内有一个FrameLayout 来扩充包含RecyclerView 的片段。问题是,我想在加载RecyclerView 时显示一个进度条,但进度条总是在Toolbar 下方的屏幕顶部。我尝试了各种布局,设置gravitycenterInParent,但没有一个有效。那么有什么办法可以实现呢?

【问题讨论】:

    标签: android android-coordinatorlayout


    【解决方案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>
    

    结果:

    【讨论】:

    • 谢谢,但这个问题与当前版本的支持库不再相关。
    • 问题是关于coordinatorlayout 而不是constraintlayout
    • @Zach 所以你只需要设置layout_gravity = "center"
    【解决方案2】:

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

    1. 将进度条放在带有android:layout_gravity="center"的CoordinatorLayout内。
    2. 在activity中,制作2个方法来显示/隐藏进度条:

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

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

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

    【讨论】:

    • 究竟“修复”了什么?以及如何?
    • @androiddeveloper 上次我检查支持库 23.1.1 时,我能够达到问题中所述的预期结果,而无需在此答案中使用“hack”。
    • 哦,我明白了。只是在我的情况下,只有在上部区域展开时它才在中心,并且我希望它在它折叠时居中,因为我默认它是折叠的......
    • @androiddeveloper 可能使用AppBarStateChangeListener 并在折叠时将paddingTop 设置为ProgressBar
    • 我已经根据里面的内容添加了边距(填充也可以)。现在可以了。谢谢。
    【解决方案3】:

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

    【讨论】:

    • 其实你不需要把CoordinatorLayout放在RelativeLayout里面,只是为了让进度条居中。 layout_gravity 可以做到这一点。我的意思是进度条在 OTHER xml 文件中,在这种情况下,不可能把它放在中心。
    【解决方案4】:
        android:layout_gravity="center" 
    

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

    【讨论】:

    • FrameLayout 是膨胀片段的视图。它不是最外面的 ViewGroup。
    【解决方案5】:

    我创建了 My View Like:

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

    【讨论】:

    • 如果您不使用片段,它可能会起作用。然而,正如我在第一篇文章中所述,我在CoordinatorLayout 中使用FrameLayout 来扩充我的片段,其中包含RecyclerViewTextViewProgressBar。在这种情况下,重力属性都不起作用。
    【解决方案6】:

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

    【讨论】:

    • 谢谢,但是进度条必须在片段的xml中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    相关资源
    最近更新 更多