【问题标题】:How to place views exactly in the center in coordinator layout?如何将视图准确地放置在协调器布局的中心?
【发布时间】:2016-08-17 07:43:18
【问题描述】:

我有一个带有回收器视图的片段和一个文本视图,当没有可用数据时会显示。片段托管在具有协调器布局的活动中。现在的问题是文本视图并不完全位于屏幕中心,但是当工具栏折叠时它位于中心。我希望 textview 正好在中心。如何做到这一点。

state]2]2

Activity的布局

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

【问题讨论】:

  • 发布您的布局 xml 会有所帮助!
  • @LalitPoptani 发布请看一下。

标签: android android-recyclerview android-coordinatorlayout


【解决方案1】:

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

    android:layout_gravity="center" 

【讨论】:

    【解决方案2】:

    在这里试试这个:

    <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】:

      尝试将其添加到您的文本视图中:

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

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        尝试框架布局。

        <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】:

          您可以做的一个非常简单的事情是简单地将底部填充添加到您的文本视图“empty_text_tv”。在您的情况下,您可以将其设置为 ?attr/actionBarSize。至少这样,当工具栏未折叠时,文本将居中。看起来好多了。

          如果您需要文本视图始终居中,则必须将 OnOffsetChangedListener 添加到您的 AppBarLayout。在此侦听器的 'onOffsetChanged' 方法中,使用参数 'verticalOffset' 并将 textview 的 translationY 设置为 verticalOffset / 2(可能使用一些委托/事件订阅技术)。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-05-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-09
            • 1970-01-01
            • 2015-09-04
            相关资源
            最近更新 更多