如何将视图准确放置在协调器布局的中心? [英] How to place views exactly in the center in coordinator layout?

查看:64
本文介绍了如何将视图准确放置在协调器布局的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有回收站视图和textview的片段,当没有可用数据时将显示该片段.片段托管在具有协调器布局的活动中.现在的问题是文本视图不完全位于屏幕中心,而是当工具栏折叠时它位于中心.我希望textview恰好在中心.怎么做.

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.

状态为正常] 2

state]2]2

活动布局

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

推荐答案

只需将其添加到视图的属性中即可:

just add this to your view's attribute :

    android:layout_gravity="center" 

这篇关于如何将视图准确放置在协调器布局的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆