Fixed aspect ratio View

后端 未结 9 1807
后悔当初
后悔当初 2020-12-04 10:50

How would I go implementing a fixed aspect ratio View? I\'d like to have items with 1:1 aspect ratio in a GridView. I think it\'s better to subclas

9条回答
  •  渐次进展
    2020-12-04 11:42

    For new users, here's a better non-code solution :

    A new support library called Percent Support Library is available in Android SDK v22 (MinAPI is 7 me thinks, not sure) :

    src : android-developers.blogspot.in

    The Percent Support Library provides percentage based dimensions and margins and, new to this release, the ability to set a custom aspect ratio via app:aspectRatio. By setting only a single width or height and using aspectRatio, the PercentFrameLayout or PercentRelativeLayout will automatically adjust the other dimension so that the layout uses a set aspect ratio.

    To include add this to your build.gradle :

    compile 'com.android.support:percent:23.1.1'
    

    Now wrap your view (the one that needs to be square) with a PercentRelativeLayout / PercentFrameLayout :

    
         
     
    

    You can see an example here.

提交回复
热议问题