前两个字段是来自 android ICS 的 SMS 应用程序,第三个字段是我的。
我这样做了:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/searchfield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/newItem"/>
<ImageButton
android:id="@+id/newItem"
android:layout_height="40dip"
android:layout_width="40dip"
android:layout_alignParentTop="@+id/searchfield"
android:adjustViewBounds="true"
android:contentDescription="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
图像设置如下:
Drawable img = getResources().getDrawable( R.drawable.light_send );
newItem.setBackgroundDrawable(img);
我绘制的箭头非常糟糕,并且在向下创建蓝色背景时遇到问题。我错过了什么吗?
最佳答案
对于 ImageButton,您要将背景设置为 color state list与您正在寻找的颜色。至于你想做的图像setImageResource而不是 setBackgroundDrawable。这样你就可以改变 ScaleType调整图像的绘制方式。我会使用 center_inside 来获得最好看的箭头。在 ImageButton 上,背景的行为与在任何 View 上的行为完全相同,图像资源是内容并绘制在背景之上。
关于java - 带图像的 android 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10336718/