Fixed aspect ratio View

后端 未结 9 1801
后悔当初
后悔当初 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:45

    I created a layout library using TalL's answer. Feel free to use it.

    RatioLayouts

    Installation

    Add this to the top of the file

    repositories {
        maven {
            url  "http://dl.bintray.com/riteshakya037/maven" 
        }
    }
    
    
    dependencies {
        compile 'com.ritesh:ratiolayout:1.0.0'
    }
    

    Usage

    Define 'app' namespace on root view in your layout

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    Include this library in your layout

    
    

    Update

    With introduction of ConstraintLayout you don't have to write either a single line of code or use third-parties or rely on PercentFrameLayout which were deprecated in 26.0.0.

    Here's the example of how to keep 1:1 aspect ratio for your layout using ConstraintLayout:

    
    
        
    
        
    
    
    

提交回复
热议问题