简体   繁体   中英

how to make widgets resize according to the screen size?

i'm designing an app and i have several buttons displayed in a table layout. I have two devices to test the app on. One is a Samsung galaxy Tab 1 and the other is a Samsung galaxy Ace. I used the following in the android manifest file to allow resizing.

<supports-screens 
android:resizeable="true" 
android:smallScreens="true" 
android:normalScreens="true" 
android:xlargeScreens="true" 
android:anyDensity="true" />  

All the buttons also have

android:layout_weight="1".

that allows them to take up extra blank space available on the display. This works fine on the tab but on the phone some of the buttons wrap. ie the word "button" is displayed on 2 lines. I want the buttons to resize themselves such that no button wraps height-wise on any display. Can someone tell me how to achieve this ? oh and the app was developed for android 2.3.3 .

There are 2 ways to implement it:

(1) Put the images of suitable sizes in xhdpi,hdpi, mdpi and ldpi folders. Devices will automatically take the images from these folders according to their resolutions.

(2) You can also detect the resolution of the device dynamically and then set the layouts accordingly, like this:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    switch(displayMetrics.densityDpi){ 
        case DisplayMetrics.DENSITY_LOW: 
            // layout for small devices.
            break; 
        case DisplayMetrics.DENSITY_MEDIUM: 
            // layout for medium devices.
            break; 
        case DisplayMetrics.DENSITY_HIGH: 
            // layout for bigger devices.
            break; 
    } 

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