开发者

Can anyone give me examples for button and textViews in Android that will be according to "Supporting Multiple Screens"?

开发者 https://www.devze.com 2023-03-16 11:55 出处:网络
I read the http:/开发者_如何转开发/developer.android.com/guide/practices/screens_support.html.com?

I read the http:/开发者_如何转开发/developer.android.com/guide/practices/screens_support.html.com? but couldn't get how to apply this, can anyone give me example with buttons and textview that how would it fit on different devices?


It's not as complicated as you might think. First remember that your basic layout is always designed for mdpi, for a normal screen size with with a medium density (about 160 dpi).

So when you design your layout the important part is that you don't use px as unit when you define your layout. Instead always use dp (Density-independent pixel) as unit as they are automatically scaled to the correct number of px for the current density.

Til this moment you only have one file (e.g. myLayout.xml) for all the different layout sizes (small, normal, large and xlarge). If you think that your layout should be different on a device with a xlarge display, like a tablet then you simply create another folder called layout-xlarge in the same level as layout and another layout file named myLayout.xml. You can now make changes to this file to let the layout look different on devices with a xlarge display. Perhaps you want a larger text box you you want to rearrange the button and text box.

So as you see, it's not that hard. Just use dp as the unit for dimensions and Android will do the rest for you.


The simplest way to support multiple screens is to place your resources in the different density folders, and layouts in different layout size folders.

For example, have the following folder structure:

AppRoot/
  res/
    drawable/
    drawable-hdpi/
  layout/
  layout-large/

You can place your bitmaps in the different drawable directories based on their densities and sizes. That sorts out different bitmaps for different density screens.

For different layouts according to screen size, in the layout and layout-large directories put the different layouts to support how you want to display your screen.

Lets take a simple example of a simple list filling screen for a small device, versus a large screen where you may want your list in the 40% of screen, and information to right of this:

layout/main_layout.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <ListView
        android:id="@+id/main_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
</LinearLayout>

layout-large/main_layout.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:weightSum="1.0"
    >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="0.4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        >
        <ListView
            android:id="@+id/main_list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/details_large"
        android:layout_weight="0.6"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        >
        <include layout="@layout/detail_information" />
    </LinearLayout>

Now your application can just use "main_layout", and the device will pick the one according to the screen size, which gives you your multiple screen support.

The important point being that the layouts are the same named file main_layout.xml, and android picks the one from the correct folder itself. The same is true of the bitmaps for buttons etc; place the same named bitmap in multiple directories and android sorts out which to use according to the screen density.

I've refrained from putting text views and buttons in these examples, because they're just layout elements that you can fill in as needed.

0

精彩评论

暂无评论...
验证码 换一张
取 消