简体   繁体   中英

Android Screen Sizes - Layout issues. Possible to just stretch rather than define 4 layouts?

I've spent a long time working on layout issues and screen sizes. I've read through the Android document here, which has helped and makes total sense: http://developer.android.com/guide/practices/screens_support.html

So, now I have my app showing exactly the same size on different screen sizes, as all of my elements throughout use DIP ( Density independant pixels ).

However, I have two devices infront of me, one larger than the other, so what that means is some empty space at the bottom of the larger screen size ( Sensation XE ) than on the slightly smaller screen size ( Desire Z ).

Reading the Android document, the general solution is to create layouts for xlarge, large, normal and small screen sizes that have a different number of DIP.

That's great, but with the layout I have which is awfully complex ( lots of custom views, lots of buttons, 3 text views, lots of images that are seperate so they can light up ), it's a lot of work to recreate those layouts.

The layout however, would be ok if it was stretched. If it took how it looks on for example the Desire Z, and literally just stretched everything to fit the larger screen, that would be fine as stretching here shouldn't be a big problem.

My question is, is there a way to do that without having to re-create the layout 4 times? Yes I realize I can copy paste most of it, but it would be really handy to just stretch what I have.

I tested on a tablet device, and it showed well but everything was tiny and didn't fill the screen. I have different sized images in the different folders ( xhdpi through to ldpi ), so it picking the correct image shouldn't be an issue.

Thanks, any help is much much appreciated.

As per Android docs, you can use a Relative Layout to satisfy your need. use Relative position for elements in Your view. You can use following attributes:

for complete list of attributes click here

Yes, in general you can stretch things to fit your layout using fill_parent for android:layout_width and/or android:layout_height on certain widgets. You can also scale proportionally by setting layout_weight appropriately for a series of widgets in a LinearLayout. How to do it really just depends on what you want to do, and is very difficult to answer in the abstract. If you have an example and a question about how to do something in particular, that might be easier to answer.

Here's an example of a RelativeLayout that uses fill_parent to handle different screen sizes:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff880000"
        android:text="Some text" />
    <TextView 
        android:id="@+id/second"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@id/first"
        android:layout_toRightOf="@id/first"
        android:background="#ff008800"
        android:text = "More text" />
    <TextView 
        android:id="@+id/third"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/first"
        android:background="#ff000088"
        android:text="Even more text" />
    <TextView 
        android:id="@+id/fourth"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/third"
        android:background="#ff008888"
        android:text="The last text" />
    </RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM