是否可以在 CoordinatorLayout 中将 layout/view 居中?
Is it possible to center a layout/view inside CoordinatorLayout?
我在 CoordinatorLayout
中有一个 FrameLayout
来膨胀我包含 RecyclerView
的片段。问题是,我想在加载 RecyclerView
时显示进度条,但进度条始终位于 Toolbar
下的屏幕顶部。我尝试了各种布局,设置 gravity
或 centerInParent
但 none 有效。那么有什么办法可以实现吗?
在 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 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>
android:layout_gravity="center"
工作正常。
并删除您的 FrameLayout,它是多余的。
将CoordinatorLayout 放入RelativeLayout 中,并在该RelativeLayout 中添加进度条。使进度条的 CenterInParent 为真。根据需要制作进度条Gone/Visible。
经过一番研究,我未能找到使这项工作可行的方法,所以最后我这样做了:
- 使用
android:layout_gravity="center"
. 将进度条放在 CoordinatorLayout 中
在activity中,对show/hide进度条制作2个方法:
public void showProgress() {
progressBar.setVisibility(View.VISIBLE);
}
public void hideProgress() {
progressBar.setVisibility(View.INVISIBLE);
}
在片段中,通过getActivity()
获取上面activity的引用,将其转换为实际的activity并调用必要的方法。
context = getActivity();
((MainActivity)context).showProgress();
编辑:这似乎已在支持库 23.1.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
中有一个 FrameLayout
来膨胀我包含 RecyclerView
的片段。问题是,我想在加载 RecyclerView
时显示进度条,但进度条始终位于 Toolbar
下的屏幕顶部。我尝试了各种布局,设置 gravity
或 centerInParent
但 none 有效。那么有什么办法可以实现吗?
在 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 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>
android:layout_gravity="center"
工作正常。 并删除您的 FrameLayout,它是多余的。
将CoordinatorLayout 放入RelativeLayout 中,并在该RelativeLayout 中添加进度条。使进度条的 CenterInParent 为真。根据需要制作进度条Gone/Visible。
经过一番研究,我未能找到使这项工作可行的方法,所以最后我这样做了:
- 使用
android:layout_gravity="center"
. 将进度条放在 CoordinatorLayout 中
在activity中,对show/hide进度条制作2个方法:
public void showProgress() { progressBar.setVisibility(View.VISIBLE); } public void hideProgress() { progressBar.setVisibility(View.INVISIBLE); }
在片段中,通过
getActivity()
获取上面activity的引用,将其转换为实际的activity并调用必要的方法。context = getActivity(); ((MainActivity)context).showProgress();
编辑:这似乎已在支持库 23.1.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>
结果: