Custom Square LinearLayout. How?

前端 未结 3 1058
醉话见心
醉话见心 2020-12-28 16:27

I create my own class for the square layout:

public class SquareLayout extends LinearLayout{

    public SquareLayout(Context context) {
        super(contex         


        
3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-28 16:37

    Edit:

    The solution below has now been deprecated, as ConstraintLayout has become the new standard and provides this functionality.

    Original Answer:

    Turns out the Android team gave us the solution, but nobody knows about it! Check out these two classes from the Percent Support Library:

    • PercentFrameLayout
    • PercentRelativeLayout

    If you want to impose the ratio of a view, you have to place it within one of these layouts. So in this case, you have to place a standard LinearLayout, not your subclass, within one of these layouts with the right aspect ratio. Example if you want to use a PercentFrameLayout:

    
         
             
         
     
    

    And there you go, all your views will be contained within a square linear layout!

    don't forget to add the gradle dependency compile 'com.android.support:percent:23.3.0' Adjust the version number as required

提交回复
热议问题