Android: Position by percentage?

社会主义新天地 提交于 2020-01-15 12:27:07

问题


I have a UI Design from my designer, and it exists of a background map with several buttons on it, positioned non-linear all over the map. Currently I position them in a RelativeLayout that is as large as the map, and use margin-left and margin-top etc in dip.

This works ok, but I also need to account for users with very small screens, that cause the map to scale down. My relative layout scales with it, but the margin values ofcourse not.

So I am wondering, how should I do this? I would prefer to layout these buttons using percentages like

left="30%"
top="50%"

Is there anything in Android that makes such a thing possible? Otherwise I have to come up with a custom layout class for that.

Visual Representation: (Ofcourse they don't actually are on 6 lines, and partially overlap in x or y position). It's actually a real (abstract) map of a building with location markers that you can press as buttons.

 -------------------------
 |    x              x   |
 |           x           |
 |                       |
 |      x                |
 |                 x     |
 |           x          x|
 -------------------------

回答1:


Here is a complicated way that does not require a custom ViewGroup. Suppose you want a button at left 30%, top 40%

FrameLayout 
   View with background, match parent
   LinearLayout orientation=horizontal, match parent 
     View layout_width=0dp, layout_weight=30, height=match_parent
     LinearLayout orientation=vertical, width=0dp, weight=70, hieght=match
       View layout_height=0dp, layout_weight=40, width=match_parent
       FrameLayout layout_height=0dp, layout_weight=60
         Button



回答2:


I use Dimension resource files put in the relevant layout- buckets so I can change margins/paddings/sizes depending on device size.

(http://developer.android.com/guide/topics/resources/more-resources.html#Dimension)

(Storing dimensions in xml file in Android)



来源:https://stackoverflow.com/questions/17029216/android-position-by-percentage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!