How to divide space equally among odd number of views in a layout using support percent library?

286 Views Asked by At

I am using support percent library in my project.

compile 'com.android.support:percent:23.1.0'

I have three ImageViews in a LinearLayout which are supposed to take the space equally (in horizontal direction). But since percent support library has PercentRelativeLayout and PercentFrameLayout only so I googled for PercentLinearLayout and found one sample for it.

Here's the layout code:

<com.example.layouts.PercentLinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/img_01"
        app:layout_widthPercent="33.3%" />  // <- what should I write here?

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/img_02"
        app:layout_widthPercent="33.3%" />  // <- and here..

    <ImageView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/img_03"
        app:layout_widthPercent="33.3%" /> // <- and here..

</com.example.layouts.PercentLinearLayout>

You can see I have assigned 33.3% space to each ImageView but there's still 0.1% space left. Although 0.1% screen space is not that significant to notice but the current approach just doesn't seem right to me.

PS: Earlier I was using weights (each ImageView had weight 1) but since it's not good for performance so I decided to go with percentages instead.

2

There are 2 best solutions below

1
On

You could've used LinearLayout with weightSum set to 3 and android:layout_weight="1" to each ImageView

0
On

If you want to do it with percent you can set weightSum to 100 and then android:layout_weight to the number you want in percent.