我有一个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>
使用ImageButton代替常规的Button。它提供了对属性android:src
的访问,并将图像设置在那里,而不是作为背景。背景图像总是适合填充,而"source"属性通过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的更新版本。我已经添加了一个新的按钮,在你开始的那个上面,尽管我使用了我的一个图像。这个特殊的图像很长,没有那么高。在常规的按钮中,它会拉伸以填充小部件,失去视角。在顶部的新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>