android layout margins with percentage

后端 未结 1 1841
傲寒
傲寒 2021-02-11 15:43

I want to set margins by percentage.. I have 4 imageviews in a linearlayout and want to set default left,right,top,bottom margins that keep same percentage for each screen size.

相关标签:
1条回答
  • 2021-02-11 16:19

    You can have invisible Views in your LinearLayouts as spacers and use the layout_weight mechanism to assign them relative size.

    Example:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    
        <Thumbnail
            android:id="@+id/thumb1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4" />
    
        <View
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:visibility="invisible"/>
    
        <Thumbnail
            android:id="@+id/thumb2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4" />
    
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题