0

I just know this is simple and in about 30 minutes time, I'll hate myself...

I have a splashscreen which consists of a static image which fills the screen. So I simply set the background attribute of whatever root view I use in my layout.

The image has a blank area over which I need to place an "I accept" button. To deal with different resolutions, I must position it using a percentage of the display height - 58% is the spot.

I can't use layout_weight because that sizes the button and absolutelayout (setting the y position in code) is deprecated.

How can I achieve this? I don't care what viewgroup is the parent and I'm fine with having "blank" views filling up space.

I am aiming to do this entirely in layout XML to keep my code clean...

Thanks!

Simon
  • 14,407
  • 8
  • 46
  • 61

2 Answers2

4

You say you can't use layout_weight, but that's your only option if you want to do it purely in XML. I don't understand why you think you can't use it anyway. Here's an example of how you might do it:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash"
    android:orientation="vertical" >

   <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="58" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="42" >

            <!-- Place buttons here -->

    </LinearLayout>
</LinearLayout>
Jason Robinson
  • 31,005
  • 19
  • 77
  • 131
  • I think Simon was concerned that applying `layout_weight` to the `Button` itself would force sizing of its height to 42% of the screen. Appling the weight to a `LinearLayout` as you suggest obviously prevents that and allows the `Button` to take its 'natural' size. – Squonk Apr 13 '12 at 16:00
0

I don't see any other way that to use a layout_weight... Also the whole class AbsoluteLayout is deprecated, so try to avoid using it. I suggest to use an LinearLayout as your rootView with a given weight_sum of 1. add another Space-filling LinearLayout width a weight of 0.58 and below your Button with wrap_content attributes. Unfortunately I cannot tell you more unless you post your xml, so that I can see, what you try to achieve. Kind of this should work:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/your_desired_background"
    android:orientation="vertical" 
    android:weight_sum="1">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".58" />
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Rafael T
  • 15,401
  • 15
  • 83
  • 144