代码之家  ›  专栏  ›  技术社区  ›  Bevor

DensityPixel似乎无法与ImageButton一起使用

  •  0
  • Bevor  · 技术社区  · 11 年前

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

    Scaling ok

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

    Scaling not ok

    xml看起来像这样(为了保持简单,我省略了除一个按钮之外的所有按钮):

    <?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>
    

    这些尝试没有奏效: 我试着用 100分贝 而不是 包装内容 。我试过了 钢筋混凝土 而不是 出身背景 我也尝试了不同的方式 scaleType(缩放类型) 属性。

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

    1 回复  |  直到 11 年前
        1
  •  1
  •   Amarjit    11 年前

    请避免使用100dp等特定尺寸。尝试使用WRAP_CONTENT或MATCH_PARENT。对于不同的屏幕大小,请使用可绘制的文件夹,如可绘制的hdpi和可绘制的mdpi等。并将android的corrosponding图像:background=“@drawbable/control_button”放在其所有特定文件夹中。

    有关多种屏幕尺寸,请参阅以下链接: http://developer.android.com/guide/practices/screens_support.html http://developer.android.com/training/multiscreen/screensizes.html