0

我的应用程序中有几个按钮没有任何文本,只有背景可绘制。我需要这样做,以便从适当的drawable-?dpi文件夹中获取可绘制对象,然后在不调整大小的情况下显示。第一部分工作,选择适当的图像文件。但是,无论按钮具有什么layout_heightlayout_width属性,它仍然会调整大小。我的问题是:如何解决这个问题,并使可绘制对象映射到屏幕像素到像素?

我可以让它不调整大小的唯一方法是在 中指定宽度和高度px,这正是我不想做的。而我不想这样做的原因是dimensions文件,我可以在其中px为不同的密度指定可绘制大小,由屏幕大小而不是密度来区分。

这是我的布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="3dp"
            android:paddingTop="3dp" >

            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/btn" />

        </RelativeLayout>
    </LinearLayout>

</ScrollView>
4

1 回答 1

1

如果您想使用 dpi 文件夹中的精确图像尺寸,您可以将 wrap_content 用于 layout_height 和 layout_width。它将占用图像定义的确切大小。

通常不建议这样做,因为有许多不同的手机具有不同的密度、分辨率等。

使用非文本图像的另一种方法是使用九个补丁将其拉伸到您需要的任何位置。

希望这可以帮助!

于 2013-03-18T15:23:35.990 回答