0

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.

Ganesh Mohan
  • 1,027
  • 10
  • 18
  • You can achieve that without using a support lib and instead use linearlayout with weight and nest your imageviews inside the linear layouts. – Skynet Oct 29 '15 at 12:47
  • 1
    make it 33.333%, then only 0.001% will be remaining. – activesince93 Oct 29 '15 at 12:49
  • 2
    "Earlier I was using weights (each ImageView had weight 1) but since it's not good for performance" -- please post your performance analysis showing that your specific use of `LinearLayout` and equal weights was "not good for performance", either on an absolute basis or in comparison to what you are trying here. **All** layouts are "not good for performance" in one way or another. – CommonsWare Oct 29 '15 at 12:49
  • @activesince93: that doesn't really answer the question. – Ganesh Mohan Oct 29 '15 at 12:50
  • @CommonsWare: Okay. Suppose I had this PercentLinearLayout in some another layout and had some weight assigned to it. Wouldn't it then be considered the case of nested weights which I believe are not good for performance, right? – Ganesh Mohan Oct 29 '15 at 12:57
  • I may be completely wrong but don't you think the performance of a 3rd party library to achieve something that is natively possible already, will be worse? – camelCaseCoder Oct 29 '15 at 12:58
  • "I believe are not good for performance" -- before ripping out and replacing a layout based on performance concerns, developers should test to see *if there are actual performance concerns*. Many of Google's performance suggestions are micro-optimizations and therefore are only really relevant in specific settings. Beyond that, since mobile device screens are rendered in pixels, it is not always going to be possible to have equal sizes for everything, because the number of pixels may not be evenly divisible by the number of items to be equally sized. – CommonsWare Oct 29 '15 at 13:08
  • @CommonsWare: *"before ripping out and replacing a layout based on performance concerns, talented developers test to see if there are actual performance concerns. Many of Google's performance suggestions are micro-optimization"* You are right. One should always profile before assuming anything. I think this is a case of "premature optimization". I guess I have to go with the weights approach then. – Ganesh Mohan Oct 29 '15 at 13:14

2 Answers2

2

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

camelCaseCoder
  • 1,447
  • 19
  • 32
  • That's exactly what I was doing earlier. I was wondering how to deal with this situation using percent support library. – Ganesh Mohan Oct 29 '15 at 12:49
0

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.