2017-06-03 31 views
0

我想在RecyclerView的行之間嵌入一個圖像(作爲另一個ViewType/ViewHolder)。所述圖像必須具有9:5的縱橫比,最大高度爲600px,因此它在較大的手機或平板電腦上不會變大,因此如果由於縱橫比導致尺寸與父寬度不匹配,則必須水平居中大小調整。Android視圖能否具有寬高比,水平填充父級並且以父級爲中心?

這是否可以實現沒有自定義代碼?我嘗試了ConstraintLayout和PercentFrameLayout,並且都似乎忽略了android:maxHeight標準。

+0

600px的最大高度 - 你實際上並不希望這樣做。你想要一個以dp爲單位的最大高度。在px中做它實際上並不能控制它的大小,在高密度的顯示器上它將是低密度顯示器的四分之一。 –

+0

@GabeSechan有效的批評,我會考慮後,實際上得到所有其他標準的工作。 – Machinarius

+0

我通常會在顯示圖像後調整圖像大小(也在RecyclerView中)。在這種情況下,我得到一個圖像寬度,計算它的高度並設置。 – CoolMind

回答

0

您可以使用指南使用ConstraintLayout來完成此操作。將您的指南600px分開設置,並將ImageView限制爲指南:頂部和底部。這些限制將會使圖像不能超越600px。下面是我在上下移動底部指南時發生的事情的一個video。以下是佈局的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="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.myapplication.MainActivity"> 

    <android.support.constraint.Guideline 
     android:id="@+id/guidelineTop" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_begin="0px" /> 

    <android.support.constraint.Guideline 
     android:id="@+id/guidelineBottom" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_begin="600px" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_margin="0dp" 
     app:layout_constraintBottom_toTopOf="@+id/guidelineBottom" 
     app:layout_constraintDimensionRatio="9:5" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/guidelineTop" 
     app:srcCompat="@mipmap/ic_launcher"/> 

</android.support.constraint.ConstraintLayout> 
相關問題