2016-09-20 2 views
1

私はRecyclerViewGridLayoutManagerを試しています。GridLayoutManager同じイメージサイズ

私は最終的にこのように表示され、同じ大きさの画像、あるいは、正方形、可能な場合に表示する

enter image description here

これを可能にするGridLayoutManagerの要素は何ですか? RecyclerViewを入力するために使用する個々のアイテムXMLを変更しますか?あなたは以下のようにImageViewをサブクラス化する必要があるすべての

+1

カスタムの行アイテムのレイアウトをカスタマイズできます – Piyush

答えて

1

まず:

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.ImageView; 


public class MyImageview extends ImageView { 
    public MyImageview (Context context) { 
     super(context); 
    } 

    public MyImageview (Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyImageview (Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); // Snap to 
     // width 
    } 
} 

XMLファイルで、このファイルを使用します。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.app.MyImageview 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/imageBackground" 
     android:scaleType="centerCrop"/> 

</LinearLayout> 

あなたMyImageView高さと幅がmatch_parentでなければならないことを確認してください。

+0

ありがとうございます。私はそれにショットをつけますが、実際にはどこに配置されていますか?あなたがしているのは親にマッチしていることだけです...あなたが間違っていると言っているのではなく、各画像を固定サイズ – tccpg288

関連する問題