首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >设置背景图像,使其不会调整大小(像素完美)

设置背景图像,使其不会调整大小(像素完美)
EN

Stack Overflow用户
提问于 2013-03-15 23:15:16
回答 1查看 2.1K关注 0票数 0

我有一个背景可绘制的Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"。按钮上没有文本。但是,它的显示大小使得可绘制对象的大小稍有调整,并且看起来模糊,除非我在px中将宽度和高度设置为可绘制对象的宽度和高度。怎样才能让wrap_content正常工作?或者任何其他不涉及硬编码按钮大小的方法,这样当可绘制的大小发生变化时,我就不需要进行额外的编辑?

下面是我的XML:

代码语言:javascript
运行
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2013-03-15 23:37:35

使用ImageButton而不是常规按钮。它提供了对属性android:src的访问,并将您的图像设置在那里,而不是作为背景。背景图像总是适合填充的,其中“源”属性通过android:scaleType属性控制图像的大小调整。请参阅:http://developer.android.com/reference/android/widget/ImageButton.htmlhttp://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

注意,对于您的ImageButton,您还需要确保android:background="@null",否则您将在源图像后面看到默认的按钮图像。

编辑:

这是您的XML的更新版本。我已经添加了一个新的按钮,虽然我使用的是我的一个图像,但在您开始时使用的是一个按钮。特定的图像很长,并不是那么高。在常规的Button中,它会伸展以填充小部件,从而丢失透视图。在顶部的新ImageButton中,它保持了自己的透视图。

代码语言:javascript
运行
复制
<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" >

            <ImageButton
                android:id="@+id/new_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:scaleType="fitCenter"
                android:src="@drawable/postwall" />

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

        </RelativeLayout>
    </LinearLayout>

</ScrollView>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15435914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档