【问题标题】:android: Button with text and background image + make transparent area not clickableandroid:带有文本和背景图像的按钮+使透明区域不可点击
【发布时间】:2015-11-06 07:29:26
【问题描述】:

如何制作一个带有背景图片和文字的按钮,同时背景图片的透明区域不可点击?

据我所知,

使用ImageButton:有背景图片(是),透明区域不可点击(是),文字(否)

使用按钮:有背景图片(是),使透明区域不可点击(不确定),文字(是)

如何才能让一个按钮获得 3 个“是”?

【问题讨论】:

  • 创建您的自定义布局

标签: android xml android-studio button imagebutton


【解决方案1】:

你可以试试 在框架布局中添加图像按钮,然后在其上方添加文本视图,这应该可以解决您的问题

【讨论】:

    【解决方案2】:

    看到一件事是使用自定义布局。为背景设置一个框架布局,添加一个居中的 textview 并使 textview 可点击,并为其附加一个侦听器以处理其点击事件

    【讨论】:

      【解决方案3】:

      好的,我明白了:使用 OnTouchListener() 并像 ImageButton 一样捕捉透明像素,然后像往常一样设置文本。

      Button btn = (Button) findViewById(R.id.btn6);
          btn.setBackground(getResources().getDrawable(R.drawable.ic_launcher));
      
          // use setOnTouchListener to make transparent area not clickable
          btn.setOnTouchListener(new View.OnTouchListener() {
              public boolean onTouch(View v, MotionEvent event) {
      
                  // get bitmap
                  Bitmap bmp = null;
                  v.setDrawingCacheEnabled(true);
                  v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snapshot(bmp)
                  try {
                      v.buildDrawingCache(); // UPDATE HERE
                      bmp = Bitmap.createBitmap(v.getDrawingCache());
                  } finally {
                      v.setDrawingCacheEnabled(false);
                  }
      
                  // get color of bitmap
                  int color = 0;
                  try {
                      color = bmp.getPixel((int) event.getX(), (int) event.getY());
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
      
                  if (color == Color.TRANSPARENT)
                      return false;
                  else {
      
                      switch (event.getAction()) {
                          case MotionEvent.ACTION_DOWN:
                              break;
                          case MotionEvent.ACTION_OUTSIDE:
                              break;
                          case MotionEvent.ACTION_CANCEL:
                              break;
                          case MotionEvent.ACTION_MOVE:
                              break;
                          case MotionEvent.ACTION_SCROLL:
                              break;
                          case MotionEvent.ACTION_UP:
                              Toast.makeText(getApplicationContext(), "ACTION UP", Toast.LENGTH_SHORT).show();
                              break;
                          default:
                              break;
                      }
                      return true;
                  }
              }
      
          });
      

      【讨论】:

        【解决方案4】:

        试试这样的,

        <RelativeLayout
                android:id="@+id/saveDeleteBar"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
        
                android:background="@color/purple">
        
                <TextView
                    android:id="@+id/txt1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/imgbtnDrVisitSave"
                    android:ellipsize="end"
                    android:gravity="left|center_vertical"
                    android:paddingLeft="5dp"
                    android:singleLine="true"
                    android:text=""
                    android:textColor="@color/white"
                    android:textSize="18dp"
                    android:textStyle="normal" />
        
                <ImageButton
                    android:id="@+id/imgbtnDrVisitSave"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:adjustViewBounds="true"
                    android:background="@null" //HERE YOU CAN ADD IMAGE
                    android:padding="5dp"
                    android:scaleType="fitCenter"
                    android:src="@drawable/yes_dark" />
        
            </RelativeLayout>
        

        【讨论】:

        • 您好,谢谢您的回答。我之前也想过这种方式,但是我要在不同的显示中多次重复使用图标按钮,所以我放弃了这种方式。
        • 你觉得如果你想多次使用,还有其他方法吗?
        • 抱歉,您的意思是自定义布局吗?我在考虑只使用一个按钮或一个 imageButton 来完成它,而不是使用 2 层视图,就像所有在一个中一样..在下面接下来的两篇文章中我的回答怎么样?不确定这是否是一种不好的做法
        • 您可以选择哪种方式。我刚刚向您展示了一种方法。但这是一种简单的实现方法,而不是让它变得复杂。
        【解决方案5】:

        我认为@amit vaghela 所说的最佳实践是考虑相对布局或线性布局,其中包含图像和文本,并使每个部分都可以点击或不按你想要的那样点击。

        【讨论】:

          猜你喜欢
          • 2013-09-20
          • 2019-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-04
          • 2014-04-30
          • 2011-09-15
          • 1970-01-01
          相关资源
          最近更新 更多