Android DensityPixel不';似乎无法使用ImageButton

Android DensityPixel不';似乎无法使用ImageButton,android,pixel,dpi,image-scaling,Android,Pixel,Dpi,Image Scaling,对于一个游戏,我有六个按钮,在mdpi上看起来不错。左侧的4个按钮为100x100px: 如果我切换到hdpi,它看起来是这样的: xml如下所示(为了保持简单,我省略了所有按钮): 这些尝试没有成功: 我尝试使用100dp而不是wrap\u content。我尝试了src而不是background,还尝试了不同的scaleType属性 也许我可以在代码中计算比例因子,但我无法想象这是正确的方法。为什么它不能自动缩放,因为dp是用来做什么的?那么如何在xml中配置正确的比率呢?请避免使用

对于一个游戏,我有六个按钮,在mdpi上看起来不错。左侧的4个按钮为100x100px:

如果我切换到hdpi,它看起来是这样的:

xml如下所示(为了保持简单,我省略了所有按钮):


这些尝试没有成功: 我尝试使用100dp而不是wrap\u content。我尝试了src而不是background,还尝试了不同的scaleType属性


也许我可以在代码中计算比例因子,但我无法想象这是正确的方法。为什么它不能自动缩放,因为dp是用来做什么的?那么如何在xml中配置正确的比率呢?

请避免使用特定的大小,如100dp。尝试使用换行内容或匹配父项。对于不同的屏幕尺寸,请使用drawable文件夹,如drawable hdpi和drawable mdpi等。并将android:background=“@drawable/control_button”的相应图像放在所有特定文件夹中

有关多个屏幕大小,请参阅以下链接:

您将图像放在哪个文件夹中(顺便说一句:src不会缩放,而背景会缩放)?它们是否按分辨率适当缩放(因为dpi很重要!)?要为分辨率配置不同的维度,您可以使用dimens.xml文件(或使用Java),但事实并非如此。您使用的似乎是平板电脑,平板电脑有不同的规则集来命名可绘制文件夹。@ArtooDetoo它只是在可绘制文件夹中。我不想要特殊尺寸的纽扣。我只是希望它有一个合适的比率。drawable是用于mdpi资源(160 dpi),主要用于手机。平板电脑遵循Android 3.2+的不同可绘制文件夹命名规则(即:对于分辨率为1920*1080、分辨率为320 dpi的平板电脑,可绘制-sw1080dp-xhdpi)。要支持早期Android版本的表,必须为drawable(即:drawable xlarge)指定旧的小、普通、大和xlarge修饰符。请参阅neeni建议的链接。drawable文件夹是默认文件夹,所以当我不需要它时,为什么要为不同的文件夹设置不同的大小?我不明白。此外,正如我所说,wrap_内容也不起作用。我现在在一个可绘制的xhdpi文件夹中放置了一个更大的图像。我不知道确切的原因,但缩放似乎适用于这两种尺寸,尽管按钮之间的间隙不再合适。我会用这个实验来找出正确的设置。是的,请使用相应的文件夹来绘制和密度
<?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>