Does Android have some equivalent to iPhones struts and springs?
I keep on struggling to find some equivalent to iPhone's struts and springs in Android. And don't say gravity :-)
Well do say gravity, but then explain how I can prevent views being pushed off the screen in LinearLayout or RelativeLayout. Or show me some other layout that allows the screen to be filled without bumping things out of sight.
In iPhone Interface builder I would just set the springs appropriately to have each view take up as much space as is available but no more. This allows iPhone layouts to handle orientation changes real good.
In Android the main approach I read about seems to be to create multiple layout folders like layout-port and layout-land and then replicate XML layout files across them, but that seems highly error prone, and still the only way I can stop get a large custom view from pushing other buttons off the screen is to set its layout_height precisely for a particular screen size and orientation. Given the range of Android displays hitting the market this feels like it's going to be more and more of a pain.
Here's an example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ImageView android:id="@+id/imageView1" android:src="@drawable/icon" android:layout_height="320dip" android:layout_width="fill_parent"></ImageView> <SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1" android:layout_width="fill_parent"></SeekBar> </LinearLayout>
these are all just default widgets. I defy you to find some other way to set the ImageView height so that it fills as much of the screen as possible without knocking the seekbar out of sight. Try setting the Imageview android:layout_height to fill_parent to see what I mean.
I have tried Relative Layouts and Table Layouts, and all sorts of tricks. Maybe I am missing something simple, but I'll be damned if I can find it ...
The only solution I can think of (but have yet to actually try) is to do something in code to detect the screen size and other widgets, but I am loathe to go that route since it seems like there should be a simple XML layout solution.
Try using android:layout_weight while setting the android:layout_width or android:layout_height to 0dp (depending on the parent view's android:orientation value).
There's no better equivalent in Android to what you want to do...
I had similar problem (much more of them to be honest) so I developed SpringLayout: https://github.com/sulewicz/springlayout. If I understood correctly, then I think I managed to achieve what you wanted (the image takes as much space as possible):
<?xml version="1.0" encoding="utf-8"?> <org.coderoller.springlayout.SpringLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" > <TextView android:id="@+id/A" android:layout_width="match_parent" android:layout_height="32dp" app:layout_alignParentTop="true" android:text="Test" /> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_below="@id/A" app:layout_above="@+id/seekBar1" android:src="@drawable/ic_launcher" /> <SeekBar android:id="@id/seekBar1" android:layout_width="match_parent" android:layout_height="32dp" app:layout_alignParentBottom="true" /> </org.coderoller.springlayout.SpringLayout>
Here is the screenshot