2013-04-17 32 views
6

Tôi muốn hiển thị một số thông tin cho người dùng, nhưng tôi không muốn thông tin bị loại bỏ cho đến khi người dùng chạm vào nơi khác hoặc nhấn nút quay lại. Tôi nhận ra lựa chọn hiển nhiên cho điều này là hiển thị văn bản trong một hộp thoại (trái ngược với bánh mì nướng). Tôi muốn hộp thoại của tôi giống với hệ thống Toast. Tôi nhận ra rằng tôi chỉ có thể sao chép bố cục transient_notification.xml (và các tài nguyên có liên quan của nó), nhưng vì kiểu dáng bánh mì nướng thay đổi rộng rãi theo thiết bị và hệ điều hành, điều này có vẻ không phù hợp.Android: Có cách nào tốt để tạo hộp thoại giống như bánh mì nướng không?

Vậy, có cách nào tốt để tạo Hộp thoại kế thừa kiểu dáng cho hệ thống Bánh mì nướng không?

+0

bánh mì nướng có nghĩa là hoạt động theo cách đó. Bạn có thể tạo một hộp thoại tùy chỉnh và tạo kiểu cho nó theo nhu cầu của bạn. Có chủ đề tùy chỉnh. Không thanh vân đê – Raghunandan

Trả lời

1

Bạn có thể muốn thử Crouton, tùy chỉnh và có thể được loại bỏ khi chạm.

1

Hai đề xuất có thể có. Một khả năng là sử dụng một PopupWindow và XML tùy chỉnh. Không nên quá khó để thực hiện. Một tùy chọn khác có thể là sử dụng hệ thống thay thế bánh mì nướng được tìm thấy trong dự án nguồn mở Crouton (https://github.com/keyboardsurfer/Crouton). Về cơ bản nó cung cấp một giao diện người dùng thẩm mỹ hơn, và tôi biết về các tùy chọn chờ người dùng nhấp vào trước khi loại bỏ. Bạn có thể tải xuống bản demo của họ trong cửa hàng Play.

3

hoặc bạn có thể sử dụng một cách bố trí tùy chỉnh

phương pháp Tuỳ chỉnh để hiển thị một bánh mì nướng

public static Toast currentToast; 
/** 
* Use a custom display for Toasts. 
* 
* @param message 
*/ 
public static void customToast(String message) { 
    // Avoid creating a queue of toasts 
    if (currentToast != null) { 
     // Dismiss the current showing Toast 
     currentToast.cancel(); 
    }  
    //Retrieve the layout Inflater 
    LayoutInflater inflater = (LayoutInflater) 
      context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    //Assign the custom layout to view 
    View layout = inflater.inflate(R.layout.custom_toast, null); 
    //Return the application context 
    currentToast = new Toast(context.getApplicationContext()); 
    //Set toast gravity to center 
    currentToast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0); 
    //Set toast duration 
    currentToast.setDuration(Toast.LENGTH_LONG); 
    //Set the custom layout to Toast 
    currentToast.setView(layout); 
    //Get the TextView for the message of the Toast 
    TextView text = (TextView) layout.findViewById(R.id.text); 
    //Set the custom text for the message of the Toast 
    text.setText(message); 
    //Display toast 
    currentToast.show(); 
    // Check if the layout is visible - just to be sure 
    if (layout != null) { 
     // Touch listener for the layout 
     // This will listen for any touch event on the screen 
     layout.setOnTouchListener(new OnTouchListener() {   
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // No need to check the event, just dismiss the toast if it is showing 
       if (currentToast != null) { 
        currentToast.cancel(); 
        // we return True if the listener has consumed the event 
        return true; 
       } 
       return false; 
      } 
     }); 
    } 
} 

custom_toast.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="10dip" 
    android:background="@drawable/custom_toast_shape"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:textColor="@color/blue" 
     android:textSize="16sp" 
     android:text="@string/app_name" 
    /> 

    <View 
     android:layout_gravity="center" 
     android:layout_width="fill_parent" 
     android:layout_height="2dip" 
     android:background="@color/blue" 
    /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="@color/black" 
     android:layout_marginTop="5dp" 
     android:layout_marginBottom="5dp" 
     android:maxEms="15" 
     android:gravity="center_horizontal" 
     android:id="@+id/text" 
    /> 
</LinearLayout> 

T o sử dụng này chỉ cần gọi

Utils.customToast("Just a test to see if toast is working!\nPerfect"); 

EDIT tôi đã sửa đổi phương pháp một chút. Bây giờ nó sẽ không tạo ra một hàng đợi của bánh mì nướng và nó sẽ bị loại bỏ khi chạm vào, giống như bánh mì nướng thông thường.

Nếu có ai khác có thể cải thiện, vui lòng làm điều đó :)

Các vấn đề liên quan