2014-04-15 44 views
8

Tôi đang cố gắng xây dựng một ứng dụng có các nút hình ảnh hoạt động giống như thanh tác vụ nhưng tôi không thể khiến chúng hiển thị chú giải công cụ trên báo chí dài.Chú giải công cụ hình ảnh android

<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" /> 

android: contentDescription hoạt động để lơ lửng (s-pen) nhưng nhấn lâu vẫn không làm gì cả.

+1

Check-out Context Menu tùy chọn cho tính năng này trên báo chí dài - http://www.thegeekstuff.com/2013/12/android-app-menus/ –

Trả lời

3

Không chính xác những gì bạn đang tìm kiếm nhưng nó thực hiện điều gì đó rất giống nhau.

1) Bảo đảm tầm nhìn của bạn là android:longClickable="true" (nên theo mặc định) và có một mô tả định nghĩa nội dung 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"/> 

2) Đăng ký một callback được gọi khi xem được lâu ép. Trình xử lý sẽ hiển thị mô tả nội dung dưới dạng tin nhắn chúc mừng.

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; 
      } 
     }); 
+0

Đây không phải là lý tưởng vì nó sẽ không hiển thị chú giải công cụ khi nhấp chuột dài đã được thực hiện, nhưng sẽ hiển thị nó ở vị trí thông báo Toast chuẩn (??). –

10

Đây là mã mà Support Library v7 sử dụng để hiển thị "cheat sheet" s cho các mục menu hành động:

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; 
} 
5

Với mức api 26 bạn có thể sử dụng được xây dựng trong TooltipText: XML:

android:toolTipText="yourText" 

ViewCompatDocs

Đối với l api cũ evels sử dụng ToolTipCompat, ví dụ:

TooltipCompat.setTooltipText(yourView, "your String"); 
Các vấn đề liên quan