4
  • 我有一個StaggeredGridLayoutManager的回收站視圖。
  • 在我有自定義項目/意見。
  • 每個項目都被定義爲ConstraintLayout,其中有一個圖像應該具有恆定的寬高比。
  • 但圖像仍然可以縮放以適合每個跨度的寬度。
  • 然後根據比例調整高度。

但不知何故圖像不保持高寬比。ConstraintLayout:無法縮放圖像以適合RecyclerView的比例

下面是XML文件和代碼來創建回收站視圖:

item.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:padding="10dp"> 


    <ImageView 
     android:id="@+id/image" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:adjustViewBounds="true" 
     android:scaleType="centerCrop" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintDimensionRatio="16:9" 
     tools:src="@drawable/ic_logo" /> 


    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintBottom_toBottomOf="@+id/image" 
     android:textColor="@color/white" 
     tools:text="SAMPLE" 
     android:paddingBottom="10dp" 
     android:layout_margin="5dp"/> 

</android.support.constraint.ConstraintLayout> 

activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_marginTop="5dp" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

代碼,以生成完整視圖:

adapter = new SampleAdapter(list); 
     StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL); 
     sampleRecyclerView.setLayoutManager(manager); 
     sampleRecyclerView.setAdapter(adapter); 

每個項目的寬度設置得很好,並適合每個列跨度的寬度。 但高度不保持,並且很少。

有人可以幫忙嗎?

感謝

回答

6

爲你的形象,你可以指定,而其他尺寸進行調整,以滿足其比例尺寸應符合的約束。請參閱Dimensions constraints文檔中標題爲「比率」的部分。

如果兩個尺寸均設置爲MATCH_CONSTRAINT(0dp),則也可以使用比率。在這種情況下,系統設置滿足所有約束條件的最大尺寸並保持指定的寬高比。根據另一方的尺寸約束一個特定的一面。例如,如果一個維度受到兩個目標的限制(例如寬度爲0dp並以父級爲中心),則可以通過添加W或H來限制寬度或高度。字母W(用於約束的寬度)或H(用於約束高度)的比率的前面,用逗號分隔:

 <Button android:layout_width="0dp" 
       android:layout_height="0dp" 
       app:layout_constraintDimensionRatio="H,16:9" 
       app:layout_constraintBottom_toBottomOf="parent" 
       app:layout_constraintTop_toTopOf="parent"/> 

所以,在你的情況,我想嘗試app:layout_constraintDimensionRatio="H,16:9"

此外,我不認爲你需要android:scaleType="centerCrop"adjustViewBounds,但你將不得不嘗試看看。

+0

layout_constraintDimensionRatio幫助我適合高度,謝謝 –