2016-08-20 184 views
0

我想为列表的所有列实现相同的布局高度。我使用GridLayoutManagerGridLayoutManager列高度问题

preview

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(mCon,2); 
recyclerView.setLayoutManager(layoutManager); 

,这是列中的XML文件

<?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:id="@+id/card_view" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="@dimen/padding_8dp" 
    android:layout_marginLeft="@dimen/padding_4dp" 
    android:layout_marginRight="@dimen/padding_4dp" 
    android:clickable="true" 
    android:padding="1dp" 
    card_view:cardCornerRadius="@dimen/padding_2dp" 
    card_view:cardUseCompatPadding="true"> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="0dp"> 

     <RelativeLayout 
      android:id="@+id/rel_first" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="@dimen/padding_8dp"> 

      <ImageView 
       android:id="@+id/userimg" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_marginLeft="@dimen/padding_16dp" 
       android:layout_marginTop="@dimen/padding_8dp" 
       android:background="@drawable/user_profile_default" /> 

      <RelativeLayout 
       android:id="@+id/nameRL" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="@dimen/padding_4dp" 
       android:layout_toRightOf="@+id/userimg" 
       android:orientation="vertical"> 

       <com.skcsllp.mutterfly.widgets.MfTextView 
        android:id="@+id/u_name" 
        style="@style/textview.bold" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="@dimen/padding_16dp" 
        android:layout_marginStart="@dimen/padding_16dp" 
        android:layout_marginTop="@dimen/padding_8dp" 
        android:text="Akshay" 
        android:textColor="@color/new_text_grey" 
        android:textSize="@dimen/text_size_big_16" 
        /> 

       <ImageView 
        android:id="@+id/location1" 
        android:layout_width="7dp" 
        android:layout_height="10dp" 
        android:layout_below="@+id/u_name" 
        android:layout_marginBottom="@dimen/padding_4dp" 
        android:layout_marginLeft="@dimen/padding_16dp" 
        android:layout_marginStart="@dimen/padding_16dp" 
        android:layout_marginTop="@dimen/padding_8dp" 
        android:background="@drawable/location_pin" 
        /> 

       <com.skcsllp.mutterfly.widgets.MfTextView 
        android:id="@+id/location" 
        style="@style/textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/u_name" 
        android:layout_marginLeft="@dimen/padding_4dp" 
        android:layout_marginTop="@dimen/padding_8dp" 
        android:layout_toEndOf="@+id/location1" 
        android:layout_toRightOf="@+id/location1" 
        android:text="2.5km" 
        android:textSize="@dimen/text_size_extra_extra_small_10" 
        /> 

      </RelativeLayout> 


      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:layout_below="@+id/nameRL" 
       android:layout_gravity="center" 
       android:layout_marginTop="@dimen/padding_8dp" 
       android:background="@color/grey_bottmon_tab" 
       /> 

     </RelativeLayout> 


     <RelativeLayout 
      android:id="@+id/main_rl" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/rel_first" 
      android:layout_centerHorizontal="true" 
      android:layout_marginBottom="@dimen/padding_16dp" 
      android:layout_marginTop="@dimen/padding_4dp" 
      android:gravity="center" 
      android:minHeight="80dp"> 

      <LinearLayout 
       android:id="@+id/ln_cart" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_marginLeft="@dimen/padding_16dp" 
       android:layout_marginRight="@dimen/padding_16dp" 
       android:orientation="horizontal"> 

       <ImageView 
        android:layout_width="@dimen/padding_24dp" 
        android:layout_height="@dimen/padding_24dp" 
        android:layout_marginTop="@dimen/padding_16dp" 
        android:background="@drawable/cart_icon" 
        android:padding="@dimen/padding_2dp" 
        /> 

       <com.skcsllp.mutterfly.widgets.MfTextView 
        android:id="@+id/item_name" 
        style="@style/textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/padding_8dp" 
        android:layout_marginLeft="@dimen/padding_8dp" 
        android:layout_marginTop="@dimen/padding_16dp" 
        android:text="Laptop " 
        android:textColor="@color/primary" 
        android:textSize="@dimen/text_size_big_16" 
        /> 

      </LinearLayout> 

      <LinearLayout 
       android:id="@+id/ln_cal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/ln_cart" 
       android:layout_centerHorizontal="true" 
       android:layout_marginLeft="@dimen/padding_16dp" 
       android:layout_marginRight="@dimen/padding_16dp" 
       android:layout_marginTop="@dimen/padding_4dp" 
       android:orientation="horizontal"> 

       <ImageView 
        android:layout_width="@dimen/padding_24dp" 
        android:layout_height="@dimen/padding_24dp" 
        android:background="@drawable/ic_cal" 
        /> 

       <com.skcsllp.mutterfly.widgets.MfTextView 
        android:id="@+id/date" 
        style="@style/textview" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginBottom="@dimen/padding_16dp" 
        android:layout_marginLeft="@dimen/padding_8dp" 
        android:layout_marginTop="@dimen/padding_4dp" 
        android:text=" 7 july - 8 july" 
        android:textColor="@color/new_text_grey" 
        android:textSize="@dimen/text_size_small_14" 
        /> 

      </LinearLayout> 
     </RelativeLayout> 


     <View 
      android:id="@+id/view_second" 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:layout_below="@+id/main_rl" 
      android:layout_marginTop="@dimen/padding_8dp" 
      android:background="@color/grey_bottmon_tab" 
      /> 


     <RelativeLayout 
      android:id="@+id/main_ln_sec" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/view_second" 
      android:layout_centerHorizontal="true" 
      android:gravity="center" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/number_of_provider" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_centerHorizontal="true" 
       android:gravity="center" 
       android:text="3" 
       android:visibility="gone" 
       /> 

      <TextView 
       android:id="@+id/offer_sent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_centerHorizontal="true" 
       android:gravity="center" 
       android:text="offersent" 
       android:visibility="gone" 
       /> 

      <ImageView 
       android:id="@+id/accept_image" 
       android:layout_width="@dimen/padding_32dp" 
       android:layout_height="@dimen/padding_32dp" 
       android:layout_centerHorizontal="true" 
       android:layout_marginLeft="@dimen/padding_16dp" 
       android:background="@drawable/check_mark" 
       android:gravity="center" 
       /> 
     </RelativeLayout> 


    </RelativeLayout> 
</android.support.v7.widget.CardView> 

这两列具有相同的高度,但底部蜱是越来越置于不同,当项目名称为大。任何人都可以建议我如何解决这个问题。谢谢

+0

哪一个是底部滴答?和一些截图。 – sumandas

+0

秒列绿色的勾被向上提升相比,第一个 –

回答

1

因为,你希望所有的main_rl都是相同的高度,这取决于你的使用情况,其中一种方法是将其高度固定为足够大的值,并让android:gravity="center"处理休息:

<RelativeLayout 
     android:id="@+id/main_rl" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_below="@+id/rel_first" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="@dimen/padding_16dp" 
     android:layout_marginTop="@dimen/padding_4dp" 
     android:gravity="center"> 
... 
</RelativeLayout> 

这应该适用于大多数情况。如果我真的想用这种方法过度,我会发现相邻卡之间的较大高度(在运行时通过ViewTreeObserver),然后将该值设置为另一个。我怀疑这是必要的。

+0

我试过了但它不必要的扩展视图,因为最小值为100dp。请你详细介绍一下viewTreeObserver –

+0

@ndeokar就像我说的,你也可以用'80dp',你的'minHeight'。用'ViewTreeObserver',当你的我们可以在需要的视图上调用getHeight()来获得它们的计算高度,然后在同一个函数中我们指定新的布局参数(基于我们使用'getHeight()'来查找最大高度)有意义的是逻辑智慧和性能智慧。 – Shaishav

+0

谢谢!我肯定会试试这个。 –

0

尝试将android:layout_below="@+id/view_second"更改为android:layout_alignParentBottom="true"以使底部标记总是位于父布局的底部。我居然没有IDE来检查我的答案是正确的,索里为:(

<RelativeLayout 
     android:id="@+id/main_ln_sec" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/view_second" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:gravity="center" 
     android:orientation="horizontal">