2016-04-24 336 views
1

對於RecyleView的每個項目,我正在使用android:layout_alignParentBottom作爲向下箭頭ImageView的內部RelativeLayout
但它只適用於Preview模式,當我運行應用程序它不起作用。我不知道爲什麼會發生這種情況。
我在截圖說明更多關於我的問題android:layout_alignParentBottom在運行時不起作用

在預覽
enter image description here
在運行時
enter image description here

<android.support.v7.widget.CardView 
     android:id="@+id/cv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

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

      <LinearLayout 
       android:id="@+id/item_layoutStart" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="#E8F5E9" 
       android:orientation="vertical"> 

       <ImageView 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_gravity="center_vertical|right" 
        android:clickable="true" 
        android:scaleType="fitCenter" 
        android:src="@mipmap/ic_launcher" /> 

       <ImageView 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_gravity="center_vertical|right" 
        android:clickable="true" 
        android:scaleType="fitCenter" 
        android:src="@mipmap/ic_launcher" /> 
       <ImageView 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_gravity="center_vertical|right" 
        android:clickable="true" 
        android:scaleType="fitCenter" 
        android:src="@mipmap/ic_launcher" /> 
       <TextView 
        android:id="@+id/item_tvRepeatTime" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:text="10" /> 
      </LinearLayout> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="#ff0" 
       android:layout_alignTop="@id/item_layoutStart" 
       android:layout_alignBottom="@id/item_layoutStart" 
       android:orientation="vertical" 
       android:layout_toRightOf="@id/item_layoutStart"> 
       <TextView 
        android:id="@+id/item_tvTitle" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="aa aa aaaaaa aa aa aaaaaa aa aa aaaaaa " /> 
       <TextView 
        android:id="@+id/item_tvResult" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/item_tvTitle" 
        android:text="aa aa aaaaaa aa aa aaaaaa aa aa aaaaaa" /> 
       <ImageView android:id="@+id/item_ivExpand" 
        android:layout_width="30dp" 
        android:layout_height="30dp" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentBottom="true" 
        android:padding="3dp" 
        android:src="@drawable/selector_arrow_down"/> 
      </RelativeLayout> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> 

UPDATE 如果我解決我的RelativeLayout通過具體的值高度,android:layout_alignParentBottom工作

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

但對我來說,我想RelativeLayout高度等於LinearLayout

<RelativeLayout 
       android:layout_alignTop="@id/item_layoutStart" 
       android:layout_alignBottom="@id/item_layoutStart" 
       > 

android:layout_alignParentBottom不行

任何幫助將是巨大的aprreciated

+0

用android:layout_gravity =「right | bottom」替換以下行:android:layout_gravity =「right」in item_ivExpand。 –

+0

將'ImageView'的父親'RelativeLayout'的'layout_height'改爲'match_parent'。 –

+0

@SagarTrehan謝謝,但它不起作用。我的問題仍然發生在 –

回答

1

雖然這種解決方案顯然與食堂在編輯器中佈局的方式,它解決了問題運行時佈局。

首先,將外部RelativeLayout更改爲LinearLayout。然後,在內部RelativeLayout上,完全刪除layout_alignToplayout_alignBottom屬性,並將layout_height設置爲match_parent

正如你可以在貼截圖看到,ImageView正在佈局如同其父RelativeLayout的高度環繞其子View S,按照其layout_height設置。然而,在RelativeLayout的小孩View被佈置之後,其layout_alignToplayout_alignBottom屬性正在被應用,並且被調整大小以匹配LinearLayout的高度,但是ImageView剛剛停留在原來的位置。

通過使用LinearLayout的外ViewGroup,和內RelativeLayout設置的高度match_parentRelativeLayout已申請一定的高度,使其奠定了之前其子View s,並且ImageViewlayout_alignParentRightlayout_alignParentBottom屬性登陸它在正確的位置。

相關問題