Fixed aspect ratio View

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

    You may find third-party libraries. Instead of using them, use constraint layout. Below code sets the aspect ratio of ImageView as 16:9 regardless of the screen size and orientation.

    
    
        
    
    
    

    app:layout_constraintDimensionRatio="H,16:9". Here, height is set with respect to the width of the layout.

    For your question, set android:layout_width="match_parent" and use app:layout_constraintDimensionRatio="H,1:1" in your view.

提交回复
热议问题