2015-11-07 76 views
0

所以我想要做的是在每張卡片內部都有一個帶有圖像的回收器視圖。Android上的Cardview尺寸

我怎樣才能知道每個卡的寬度,以便我可以將圖像傳遞給具有確切大小的適配器並佔用整個卡的空間? (我希望圖像的尺寸完全相同)。

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

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


     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="160dp" 
      android:id="@+id/phone_card_imageView" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true"/> 


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

這是我在cardview上的代碼。

回答

0

layout_widthlayout_height更改爲dp中的固定尺寸,以便您的ImageView這樣做。 請注意,您也可以只固定高度,寬度將是屏幕的一個。

+0

這並沒有解決問題,但我做到了卡的大小,現在我確切知道我需要什麼樣的圖像 –

+0

哦,我沒有看到你的卡有match_parent。列表項目的佈局應該總是將其內容包裝爲高度,或者至少需要修正大小,所以在這裏您遇到了問題 –