【问题标题】:android button with image带有图像的 android 按钮
【发布时间】:2023-03-25 23:50:01
【问题描述】:

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

我得到了一个非常糟糕的绘制箭头,并且我在创建向下的蓝色背景时遇到了问题。我错过了什么吗?

【问题讨论】:

    标签: java android image button drawable


    【解决方案1】:

    对于 ImageButton,您希望将背景设置为 color state list,并使用您正在寻找的颜色。至于你想要做一个setImageResource 而不是setBackgroundDrawable 的图像。这样您就可以更改ScaleType 来调整图像的绘制方式。我会使用 center_inside 来获得最好看的箭头。在 ImageButton 上,背景的行为与在任何视图上的行为完全相同,图像资源就是内容并绘制在背景之上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2014-09-20
      相关资源
      最近更新 更多