10

我正在为我的自定义按钮设置背景图像。我为我的应用程序制作了一个屏幕截图,并提到当我使用“wrap_content”作为按钮的宽度和高度时,按钮会被拉伸。当我在 dp 中固定大小时,它会看起来不错(例如:在我的 mdpi 文件夹中,我有 48x48px 的图标,所以我在宽度和高度上放置了 48dp)。你能解释一下为什么吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/definition_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp"
android:layout_alignParentTop="true" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttons"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp" >

    <Button
        android:id="@+id/addToFavorites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/favorites_button" />

    <Button
        android:id="@+id/fontup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/fontup_button" />

    <Button
        android:id="@+id/fontdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/fontdown_button" />

    <Button
        android:id="@+id/comment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/comment_button" />

    <Button
        android:id="@+id/speak"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/speak_button" />
    </LinearLayout>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_below="@id/buttons">

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

    <ImageView
        android:id="@+id/image_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <WebView
        android:id="@+id/webView1"
        android:layout_below="@id/image_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </LinearLayout>
    </ScrollView>

    </RelativeLayout>

上面的代码显示了我的 XML 文件。您可以查看此链接以查看我的问题的屏幕截图: https ://www.dropbox.com/s/phnxt5p335ltqef/buttons_icon_altered.png

4

3 回答 3

21

您可以通过将 minWidth 和 minHeight 设置为 0dp 来避免按钮背景拉伸

<Button
    android:id="@+id/addToFavorites"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp"
    android:layout_margin="5dp"
    android:background="@drawable/favorites_button" />
于 2014-05-20T12:17:13.887 回答
8

It is pretty easy:

  • Use ImageButton instead of Button
  • set the background attribute to null
  • set the src attribute
  • set the scaleType attribute to centerInside

Working example:

<ImageButton
android:id="@+id/my_awesome_button"
android:scaleType="centerInside"
android:background="@null"
android:gravity="center"
android:layout_height="wrap_content" 
android:layout_width="wrap_content"
android:src="@drawable/my_awesome_drawable"/>

Note: You can also use a dp value instead of "wrap_content".

I hope it helps.

于 2015-07-30T09:37:41.807 回答
5

It appears stretched because "wrap_content" means android will stretch the image to fit the enclosing layout.

In your case, the buttons in the linear layout will take as much space as they can within the layout. So unless you set the height and width for the buttons, they will stretch out to fill the space occupied by the linear layout and consequently, their background images will also stretch along with the buttons.

But if you set the button size to 48dp, then the images won't have to stretch since they are also 48px in size and hence they will look nice.

Go through the excellent official developer docs for more info on layouts here: https://developer.android.com/guide/topics/ui/declaring-layout.html

and for learning how to manage your layouts and assets for multiple android screen sizes and resolutions here:

https://developer.android.com/guide/practices/screens_support.html

于 2012-10-09T07:12:01.620 回答