Question

For a game I have six buttons which look good on mdpi. The 4 left buttons are 100x100px:

Scaling ok

If I switch to hdpi, it looks like this:

Scaling not ok

The xml looks like this (I omitted all but one button to keep it simple):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gameContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:orientation="vertical" >

        <com.mydomain.mygame.game.GameSurface
            android:id="@+id/gameSurface"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/background_game" >
        </com.mydomain.mygame.game.GameSurface>

        <RelativeLayout
            android:id="@+id/controlButtonContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <ImageButton
                android:id="@+id/button_left"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="100dp"
                android:contentDescription="@string/button_left"
                android:background="@drawable/control_button" />

        </RelativeLayout>
    </FrameLayout>

    <LinearLayout
        android:id="@+id/emptyBar"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:baselineAligned="false"
        android:gravity="center"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

These attempts didn't work: I tried to use 100dp instead of wrap_content. I tried src instead of background as well as I tried different scaleType attributes.

Probably I could calculate the scaling factor in code, but I can't imagine that this is the proper way to do that. Why does it not scaled automatically, because what are dp then for? So how can I configure the right ratio in xml?

Was it helpful?

Solution

Please avoid use of specific size like 100dp . Try to use WRAP_CONTENT or MATCH_PARENT . and for different different screen sizes please use drawable-folders like drawable-hdpi and drawable-mdpi etc . And Put corrosponding images of android:background="@drawable/control_button" in all its specific folders .

Refer these linkes for multiple screen sizes : http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/training/multiscreen/screensizes.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top