在加权布局中强制平方形式的drawable

时间:2014-07-07 10:47:32

标签: android android-layout screen android-xml android-layout-weight

我有一个水平方向的线性布局和三个按钮。为了让他们在每个屏幕上占用相同的空间,我将宽度设置为0dp,将layout_weight设置为1以使其占用的空间相等。

除了高度未正确调整外,这很好用。我的drawable是平方的,而layout_height="90dp"在Nexus 4上看起来很好,它可以挤在Nexus 7上。在这种情况下我怎么能总是得到正确的高度?

<LinearLayout
    android:layout_marginTop="30dp"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_margin="15dp"
        android:layout_width="0dp"
        android:layout_height="90dp"
        android:layout_weight="1"
        android:background="@drawable/my_squared_drawable"
        android:scaleType="fitCenter" />

    ...
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

根据定义,background属性拉伸,并且它不使用scaleType修饰符。
您最好使用属性src

但是你需要一个 ImageButton (或一个ImageView,它也是可点击的),而不是一个Button。
因此,容器将以百分比均匀分布,而内部图像将遵循scaleType修饰符。


按钮没有src属性,这就是您所需要的 而ImageButton(和ImageView)的确如此 虽然使用ImageView作为按钮可能会出现奇怪,但使用ImageButton是一种非常常见的做法