use*_*471 16 xml android tooltip imagebutton
我正在尝试构建一个带有图像按钮的应用程序,它像操作栏一样工作,但我无法让它们在长按时显示工具提示.
<ImageButton
android:id="@+id/editUrgent"
style="?android:attr/borderlessButtonStyle"
android:layout_width="48dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/editImportant"
android:hint="@string/hint_urgent"
android:contentDescription="@string/hint_urgent"
android:text="@string/hint_urgent"
android:src="@drawable/clock_light" />
Run Code Online (Sandbox Code Playgroud)
android:contentDescription适用于悬停(s-pen)但长按仍然无效.
Ast*_*ixR 22
使用api level 26,您可以使用内置的TooltipText:Via XML:
android:toolTipText="yourText"
Run Code Online (Sandbox Code Playgroud)
如果你的minSdk低于26,请使用ToolTipCompat,例如:
TooltipCompat.setTooltipText(yourView, "your String");
Run Code Online (Sandbox Code Playgroud)
不幸的是,你的XML文件中没有办法做到这一点.
Man*_*our 10
这是支持库v7用于显示操作菜单项的"备忘单"的代码:
public boolean onLongClick(View v) {
if (hasText()) {
// Don't show the cheat sheet for items that already show text.
return false;
}
final int[] screenPos = new int[2];
final Rect displayFrame = new Rect();
getLocationOnScreen(screenPos);
getWindowVisibleDisplayFrame(displayFrame);
final Context context = getContext();
final int width = getWidth();
final int height = getHeight();
final int midy = screenPos[1] + height / 2;
int referenceX = screenPos[0] + width / 2;
if (ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_LTR) {
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
referenceX = screenWidth - referenceX; // mirror
}
Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);
if (midy < displayFrame.height()) {
// Show along the top; follow action buttons
cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX, height);
} else {
// Show along the bottom center
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
}
cheatSheet.show();
return true;
}
Run Code Online (Sandbox Code Playgroud)
以 xml 工作并支持 minSdk 小于 26 的解决方案
@BindingAdapter("tooltipTextCompat")
fun bindTooltipText(view: View, tooltipText: String) {
TooltipCompat.setTooltipText(view, tooltipText)
}
Run Code Online (Sandbox Code Playgroud)
以及layout.xml中的用法
app:tooltipTextCompat="@{@string/add_for_all_tooltip}"
Run Code Online (Sandbox Code Playgroud)
不完全是您正在寻找的东西,但它的作用非常相似。
1) 确保您的视图android:longClickable="true"(默认情况下)并且具有定义的内容描述android:contentDescription="@string/myText":
<ImageButton android:id="@+id/button_edit"
android:contentDescription="@string/myText"
android:src="@drawable/ic_action_edit"
android:onClick="onEdit"
android:longClickable="true"/>
Run Code Online (Sandbox Code Playgroud)
2)注册长按视图时调用的回调。处理程序会将内容描述显示为 Toast 消息。
findViewById(R.id.button_edit).setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(context,view.getContentDescription(), Toast.LENGTH_SHORT).show();
return true;
}
});
Run Code Online (Sandbox Code Playgroud)