CardView center_vertical中的RecyclerView

时间:2015-09-30 10:01:58

标签: android android-layout center android-recyclerview android-cardview

你好我在CardView中有一个RecyclerView。 我希望在CardView中将RecyclerView文本放在center_vertical中。

CardView xml是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/item_breakfast"
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_breakfast_height"
        card_view:cardCornerRadius="@dimen/item_breakfast_corner">

        <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!-- Icon breakfast -->
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/breakfast_icon"
                android:layout_width="@dimen/item_breakfast_circleViewSize"
                android:layout_height="@dimen/item_breakfast_circleViewSize"
                android:layout_centerVertical="true"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/list_food"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/breakfast_icon"
                android:layout_centerInParent="true"
                android:scrollbarStyle="insideOverlay"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

RecyclerView xm是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true">
    <!-- Food  -->
    <TextView
        android:id="@+id/food"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

ID为“food”的TextView不是center_Vertical。为什么呢?

2 个答案:

答案 0 :(得分:0)

使用layout_gravityTextView置于布局中心:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true">

    <!-- Food  -->
    <TextView
        android:id="@+id/food"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" />
</LinearLayout>

答案 1 :(得分:0)

  

此代码可以帮助您解决问题。更改或修改您的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_breakfast"
      card_view:cardCornerRadius="@dimen/item_breakfast_corner">



      <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">


            <!-- Icon breakfast -->
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/breakfast_icon"
                android:layout_width="@dimen/item_breakfast_circleViewSize"
                android:layout_height="@dimen/item_breakfast_circleViewSize"
                android:layout_centerVertical="true"/>

            <android.support.v7.widget.RecyclerView
                android:id="@+id/list_food"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center_vertical"
                android:layout_toRightOf="@+id/breakfast_icon"
                android:layout_centerInParent="true"
                android:scrollbarStyle="insideOverlay"/>

    </RelativeLayout>