How to place views exactly in the center in coordi

2019-09-10 02:06发布

问题:

I have a fragment with recycler view and a textview which will be shown when no data is available. fragment is hosted in the activity which is having coordinator layout. Now the problem is that text view is not exactly in center of screen but when toolbar collapse it comes in center. I want the textview exactly in the center. How to do that.

state]2]2

Activity's Layout

<android.support.design.widget.CoordinatorLayout 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"
    tools:context=".common.ui.BaseActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">
        </android.support.v7.widget.Toolbar>


    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/home_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

<FrameLayout
    android:id="@+id/navigation_view"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="left" />

FRAGMENT==><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_favourite">
</android.support.v7.widget.RecyclerView>
    <TextView
        android:id="@+id/empty_text_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:visibility="gone"
        android:padding="@dimen/margin"
        android:text="@string/empty_string_fav"
        android:layout_centerInParent="true" />
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        android:id="@+id/progress_bar"/>
</RelativeLayout>`

回答1:

just add this to your view's attribute :

    android:layout_gravity="center" 


回答2:

Here try this:

<FrameLayout 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">
<LinearLayout
        android:id="@+id/llEmpty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical">
<TextView
        android:id="@+id/empty_text_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:visibility="gone"
        android:padding="@dimen/margin"
        android:text="@string/empty_string_fav"
        android:gravity="center"/>
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:visibility="gone"
        android:id="@+id/progress_bar"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/recycler_favourite">
</android.support.v7.widget.RecyclerView>


</FrameLayout>


回答3:

try to add this on your textview:

android:layout_centerVertical="true"
android:layout_centerHorizontal="true"

hope it helps.



回答4:

Try framelayout.

<FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:gravity="center">

      <ProgressBar
          android:id="@+id/progressBar"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_gravity="center" />

      <TextView
          android:id="@+id/empty"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:textColor="@color/black" />
</FrameLayout> 


回答5:

A really simple thing you can do is to simply add the bottom padding to your textview 'empty_text_tv'. In your case you can set it to ?attr/actionBarSize. At least this way the text will be centered when the toolbar is not collapsed. It looks much better.

If you need the textview be always centered, you have to addOnOffsetChangedListener to your AppBarLayout. In this listener's 'onOffsetChanged' method take the argument 'verticalOffset' and set the textview's translationY to verticalOffset / 2 (probably with some delegation/event subscription techniques).