How do I apply mathematical operations to Android dimensions?

前端 未结 4 1315
-上瘾入骨i
-上瘾入骨i 2020-12-24 11:05

How do I avoid this hardcoded math...


 10dip
 6dip
 

        
4条回答
  •  借酒劲吻你
    2020-12-24 11:14

    There's a few tricks around this, but it wouldn't be as nice as your proposed solution, which is something I want as well. For example, you can use layout paddings on not just the View (Button in this case), but you can also do it on the view's parent (the layout like LinearLayout/RelativeLayout). You can also put in invisibile Views (a straight View object works often) with fixed dimensions. It would be like

    
    

    Note that 1px is fine if want to guarantee only 1 pixel will be drawn for a dimension, which is usually what you want if you want use empty views for padding. Some say FrameLayout is better to use for empty padding, but that's descended from View

    Sometimes you can combine padding and the layout padding, but that can get messy and have your view cropped. Also you can have something like a FrameLayout or a LinearLayout contain just that view, and use that to have the added padding

提交回复
热议问题