2017-06-20 42 views
-2

我做了一個RecyclerView的佈局,其中每個項目都是一個ImageView,下面有一個TextView,下面有一個形狀來爲項目添加邊框。縮放問題RecyclerView項目

問題是,在縱向方向上,圖像不適合整個空間,而在橫向進行。

肖像例如:

景觀例如:

activity.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/coordlayout" > 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/appbarlayout"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/collapsingtoolbar" 
      app:layout_scrollFlags="scroll|enterAlways"> 

      <include 
      layout="@layout/toolbar" 
      android:id="@+id/toolbar" 
      app:layout_scrollFlags="scroll|enterAlways" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/appbarlayout" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    </android.support.v7.widget.RecyclerView> 

</android.support.design.widget.CoordinatorLayout> 

recyclerview_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 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="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="5dp" > 

    <ImageView 
     android:id="@+id/flIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:contentDescription="@string/freelancer_s_portrait" 
     android:background="@drawable/menu_square" 
     app:srcCompat="@drawable/rampart_pt" 
     android:padding="3dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <TextView 
     android:id="@+id/flName" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/menu_square" 
     android:gravity="center" 
     android:text="Asana" 
     android:textAppearance="@android:style/TextAppearance.DeviceDefault.Medium.Inverse" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/flIcon" /> 

</android.support.constraint.ConstraintLayout> 

我該如何解決?

回答

0

您可以設置imageview的scaleType屬性,但圖片可能會失真。

android:scaleType="fitXY" 
+0

你知道爲什麼視圖不只是調整其高度,所以圖像不需要拉伸,而是? –

0

如果使用wrap_contentImageView將有實際的圖像JPG,而不是你所期望的高度的高度,您可以使用

android:scaleType="centerCrop"

但是你可以使用你自己的身高DPS。