java - 带图像的 android 按钮

标签 java android image button drawable

buttons

前两个字段是来自 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/

相关文章:

java - 除非调整窗口大小,否则组件不会显示 - Java

java - 通过 vb.net 将焦点设置为 webbrowser 内的 java-inputdialog

Android: Root View 的快照(带有 ActionBar)

html - Svg 图像不显示

image - flutter : The argument type 'Image' can't be assigned to the parameter type 'IconData'

ios - 未呈现分段控件中的图像

java - JavaFX 中选项卡关闭时的双选项卡更改事件

java - 如何使用 hibernate session ?

android - 更改 Nativescript Listview `onItemTap` 背景颜色?

java - 传递给 List 的 ArrayAdapter 的 arraylist 应该是线程安全的吗?