【问题标题】:Android add image to buttonAndroid将图像添加到按钮
【发布时间】:2012-09-11 14:32:14
【问题描述】:

我想将图像添加到Button(不是imagebutton,因为特定按钮可以包含文本或图像;另外,imagebutton 也有同样的问题),同时仍保留原始的 android 样式按钮.所以在 XML 中添加 android:background 并不会减少它,因为这将删除带有圆角等的 android 默认按钮。(android.R.drawable.btn_default)

这有可能吗?

我认为一种方法是为按下的按钮制作9patch 图像(一个用于向上,一个用于向下),onTouch Action_DOWN 使分层背景可绘制并将其放在按钮上,并做同样的事情,但另一个9patch 用于onTouch Action_UP,但我认为这将大大降低应用程序的性能,因为这将需要大量资源读取和所有按钮单击的层合并(对于我的应用程序,这将是非常多)。我上面说的对吗?

编辑:我不能在 XML 中声明图像的来源,因为我从 Web 服务获取图像,所以任何东西都可以放在按钮上,但它必须以编程方式发生。

【问题讨论】:

标签: android image button


【解决方案1】:

这可以使用 layout.xml 中的android:drawableTopandroid:drawableBottomandroid:drawableLeftandroid:drawableRight 属性来完成。

【讨论】:

  • 在代码中,您可以使用 - 奇怪命名的 - 方法 setCompoundDrawablesWithIntrinsicBounds(int, int, int, int),如下所述:developer.android.com/reference/android/widget/…, int, int, int)
  • 刚读到您从网络服务获取图像,因此您最好使用此方法:setCompoundDrawablesWithIntrinsicBounds (Drawable left, Drawable top, Drawable right, Drawable bottom)。您可以使用Drawable.createFromStream(InputStream is) 创建可绘制对象。
  • 如何调整这样使用的图像大小?
【解决方案2】:

     <Button
            android:layout_width="0dp"
            android:layout_weight="1"
            android:background="@drawable/home_button"
            android:drawableLeft="@android:drawable/ic_menu_edit"
            android:drawablePadding="6dp"
            android:gravity="left|center"
            android:height="60dp"
            android:padding="6dp"
            android:text="AndroidDhina"
            android:textColor="#000"
            android:textStyle="bold" />

【讨论】:

    【解决方案3】:

    在Button的属性下方设置Display Image with Text,使用该属性图片在文字上方显示。

    <Button android:drawableTop="@drawable/ic_launcher"/>
    

    【讨论】:

      【解决方案4】:

      设置图片:

      btn.setBackgroundResource(R.drawable.ic_launcher);
      

      设置文字:

      btn.setText("My Button");
      

      代码:

      private Drawable buttonDrawable;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      
          final Button btn=(Button) findViewById(R.id.button1);
          buttonDrawable=btn.getBackground();
          //Setting the image.
          btn.setBackgroundResource(R.drawable.ic_launcher);
          btn.setOnClickListener(new View.OnClickListener() {
      
              public void onClick(View v) {
                  /*Removing image and setting text*/
                  btn.setBackgroundDrawable(buttonDrawable);
                  btn.setText("My Button");   
              }
          });
      }
      

      【讨论】:

      • 不起作用,因为我想保留 Android 风格的按钮。当我设置背景时,android 原版消失了,取而代之的是一个可能是方形的按钮。我想要按钮上/中的图像,而不是原始按钮布局
      【解决方案5】:

      您可以创建自己的具有多种状态的可绘制对象并将其设置为背景

      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:state_enabled="false" 
              android:drawable="@drawable/selected" />
          <item android:drawable="@drawable/normal" />
      </selector>
      

      Drawables 可以有这样的圆角。

      <corners android:topLeftRadius="5dp" android:topRightRadius="5dp"
               android:bottomLeftRadius="0.2dp" android:bottomRightRadius="0.2dp"/>
      

      【讨论】:

        猜你喜欢
        • 2020-08-24
        • 2018-01-23
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多