Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 879
Sam Joseph Avatar asked Nov 15 '22 02:11

Sam Joseph


1 Answers

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...

like image 81
ofirbt Avatar answered Dec 18 '22 19:12

ofirbt