android - 按钮背景图像拉伸(stretch)(wrap_content 或 dp 使用)

标签 android button background-image android-linearlayout

我正在为我的自定义按钮设置背景图片。我为我的应用程序制作了一个屏幕截图,并提到当我对按钮的宽度和高度使用“wrap_content”时,按钮被拉伸(stretch)了。当我在 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

最佳答案

您可以通过将最小宽度和最小高度设置为 0dp 来避免按钮背景拉伸(stretch)

<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" />

关于android - 按钮背景图像拉伸(stretch)(wrap_content 或 dp 使用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12794472/

相关文章:

android - 在带有 alpha 的 Material Button 上设置 backgroundTint 在正常和按下状态下具有奇怪的视觉效果

android - XML android 中带有圆底的背景图像

html - div : believe it to be due to the svg background 内的按钮不可点击

jquery - 我的背景图像被动画 div block 移动

android - java.lang.NoSuchMethodError : No virtual method elapsedNow()D in class Lkotlin/time/TimeMark

android - 安卓平板视频制作软件

c# - 如何在 C# 中向 winforms 程序添加功能?

html - 如何使用 CSS 规范化按钮和 anchor ?

css - 在处理 Retina 大小的图像时如何应用替代文本?

css - 使用 CSS 定位背景图片