Подтвердить что ты не робот

Можно ли центрировать макет/представление внутри CoordinatorLayout?

У меня есть FrameLayout внутри CoordinatorLayout, чтобы раздуть мои фрагменты, содержащие RecyclerView. Дело в том, что я хочу отображать индикатор выполнения при загрузке RecyclerView, но индикатор выполнения всегда находится в верхней части экрана под Toolbar. Я пробовал различные макеты, установив gravity или centerInParent, но никто не работал. Так есть ли способ достичь этого?

4b9b3361

Ответ 1

После некоторых исследований я не смог найти способ сделать эту работу, поэтому в конце концов я сделал это:

  • Поместите индикатор выполнения внутри координатораLayout с помощью android:layout_gravity="center".
  • В этой операции сделайте 2 метода, чтобы показать/скрыть индикатор выполнения:

    public void showProgress() {
          progressBar.setVisibility(View.VISIBLE);
    }
    
    public void hideProgress() {
          progressBar.setVisibility(View.INVISIBLE);
    }
    
  • В фрагменте получить ссылку на вышеуказанную активность через getActivity(), передать ее действительной активности и вызвать необходимый метод.

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

EDIT: это, по-видимому, исправлено в библиотеке поддержки 23.1.1

Ответ 2

    android:layout_gravity="center" 

работает просто отлично. И удалите свой FrameLayout, он лишний.

Ответ 3

Если вы собираетесь центрировать вид внутри другого, вам нужно добавить ограничения со всех сторон:

    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

Ответ 4

Я создал 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>

введите описание изображения здесь

Ответ 5

Поместите координаторLayout внутри RelativeLayout и добавьте индикатор прогресса внутри этого RelativeLayout. Сделайте CenterInParent для progressbar равным true. Сделайте прогрессbar Gone/Visible по необходимости.

Ответ 6

Использовать 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>