我有一个背景可绘制的Button
,android:layout_width="wrap_content"
和android:layout_height="wrap_content"
。按钮上没有文本。但是,它的显示大小使得可绘制对象的大小稍有调整,并且看起来模糊,除非我在px
中将宽度和高度设置为可绘制对象的宽度和高度。怎样才能让wrap_content
正常工作?或者任何其他不涉及硬编码按钮大小的方法,这样当可绘制的大小发生变化时,我就不需要进行额外的编辑?
下面是我的XML:
<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>
发布于 2013-03-15 23:37:35
使用ImageButton而不是常规按钮。它提供了对属性android:src
的访问,并将您的图像设置在那里,而不是作为背景。背景图像总是适合填充的,其中“源”属性通过android:scaleType
属性控制图像的大小调整。请参阅:http://developer.android.com/reference/android/widget/ImageButton.html和http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType
注意,对于您的ImageButton,您还需要确保android:background="@null"
,否则您将在源图像后面看到默认的按钮图像。
编辑:
这是您的XML的更新版本。我已经添加了一个新的按钮,虽然我使用的是我的一个图像,但在您开始时使用的是一个按钮。特定的图像很长,并不是那么高。在常规的Button中,它会伸展以填充小部件,从而丢失透视图。在顶部的新ImageButton中,它保持了自己的透视图。
<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>
https://stackoverflow.com/questions/15435914
复制相似问题