[英]android: Button with text and background image + make transparent area not clickable
How can I make a button with background image and text on top of it, at the same time the transparent area of background image is not clickable? 如何制作一个带有背景图片和顶部文本的按钮,同时无法单击背景图片的透明区域?
As I know, 我所知,
use ImageButton: have background image(yes), make transparent area not clickable(yes), text(no) 使用ImageButton:具有背景图像(是),使透明区域不可单击(是),文本(否)
use Button: have background image(yes), make transparent area not clickable(not sure), text(yes) 使用按钮:具有背景图片(是),使透明区域不可单击(不确定),文本(是)
How can I have 3 'yes' for a single button? 单个按钮如何有3个“是”?
您可以尝试在“框架布局中添加图像”按钮,然后在其上方使用textview,这应该可以解决您的问题
See one thing would be use a custom layout . 看到一件事就是使用自定义布局。 Take a framelayout for the background , add a centered textview and make just the textview clickable and attach a listener to it for handling its click event 为背景设置框架布局,添加居中的textview并使该textview可单击,并在其上附加一个侦听器以处理其click事件
OK I got it: use OnTouchListener() and catch the transparent pixel just like ImageButton, and set text as usual. 好的,我明白了:使用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;
}
}
});
try something like this, 尝试这样的事情,
<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>
我认为@amit vaghela所说的最佳实践是考虑其中带有图像和文本的RelativeLayout或LinearLayout并使每个部分都可单击或不按您想要的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.