2016-04-05 36 views
9

Gần đây, tôi đã sử dụng android.support.design.widget.BottomSheetDialogFragment. Tôi muốn làm một cái gì đó tương tự như ứng dụng liên lạc của Google, BottomSheet của nó có thể che phủ thanh công cụ và thanh trạng thái. Tuy nhiên, khi tôi sử dụng BottomSheetDialogFragment để thực hiện điều này, nó chỉ ra điều này: My ImplementationLàm cho toàn màn hình dướiSheetDialog trên thanh trạng thái

Như bạn có thể thấy thanh công cụ của hoạt động vẫn hiển thị. Đây là mã của tôi về BottomSheetDialogFragment:

public class KeyDetailFragment extends BottomSheetDialogFragment { 
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback() { 
     @Override 
     public void onStateChanged(@NonNull View bottomSheet, int newState) { 
      if (newState == BottomSheetBehavior.STATE_HIDDEN) { 
       dismiss(); 
      } 
     } 

     @Override 
     public void onSlide(@NonNull View bottomSheet, float slideOffset) { 

     } 
    }; 

    @Override 
    public void setupDialog(Dialog dialog, int style) { 
     super.setupDialog(dialog, style); 
     View contentView = View.inflate(getActivity(), R.layout.sheet_key, null); 
     dialog.setContentView(contentView); 
     View parent = (View) contentView.getParent(); 
     parent.setFitsSystemWindows(true); 
     BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent); 
     contentView.measure(0, 0); 
bottomSheetBehavior.setPeekHeight(contentView.getMeasuredHeight()); 

     CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) parent.getLayoutParams(); 
     if (params.getBehavior() instanceof BottomSheetBehavior) { 
       ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
     } 
     params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; 
     parent.setLayoutParams(params); 
    } 
} 

tôi đề cập đến nguồn và tôi thấy một thuộc tính quan tâm tôi:

private static int getThemeResId(Context context, int themeId) { 
    if (themeId == 0) { 
     // If the provided theme is 0, then retrieve the dialogTheme from our theme 
     TypedValue outValue = new TypedValue(); 
     if (context.getTheme().resolveAttribute(
       R.attr.bottomSheetDialogTheme, outValue, true)) { 
      themeId = outValue.resourceId; 
     } else { 
      // bottomSheetDialogTheme is not provided; we default to our light theme 
      themeId = R.style.Theme_Design_Light_BottomSheetDialog; 
     } 
    } 
    return themeId; 
} 

thuộc tính bottomSheetDialogTheme đây có thể thay đổi phong cách tấm đáy nhưng tôi không biết cách thay đổi nó, và tôi nghi ngờ liệu điều này có hiệu quả hay không. Ai đó có thể cho tôi giải pháp về việc đạt được nó mà nó có thể che phủ thanh công cụ và thanh trạng thái?

+0

Vấn đề tương tự, bạn có tìm thấy giải pháp nào cho vấn đề này không? –

+0

@ nipun.birla Không, tôi chưa tìm thấy nó mặc dù nó đã trôi qua trong khoảng 3 tháng –

+0

Tôi nghĩ lý do là độ cao. Kiểm tra điều này: http://stackoverflow.com/questions/35711808/android-bottomsheet-is-hinding-under-the-toolbar. –

Trả lời

3

Không thể tìm ra giải pháp cho vấn đề này, nhưng có thể đề xuất giải pháp thay thế giúp tôi phục vụ cùng một mục đích. Dưới đây là tham chiếu: http://www.hidroh.com/2016/06/17/bottom-sheet-everything/

Bài viết giải thích việc tạo hoạt động trang tính dưới cùng và thêm bóng nền với nó.

+0

Tham chiếu đã cho chắc chắn là một cách thay thế cho mục đích của tôi, việc thêm một bóng nền thực sự phức tạp. –

11

Hãy thử điều này. Nó làm việc cho tôi.

@Override 
public void setupDialog(Dialog dialog, int style) { 
    super.setupDialog(dialog, style); 
    View inflatedView = View.inflate(getContext(), R.layout.fragment_coupon, null); 
    dialog.setContentView(inflatedView); 


    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) inflatedView.getParent()).getLayoutParams(); 
    CoordinatorLayout.Behavior behavior = params.getBehavior(); 

    if (behavior != null && behavior instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
    } 

    View parent = (View) inflatedView.getParent(); 
    parent.setFitsSystemWindows(true); 
    BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(parent); 
    inflatedView.measure(0, 0); 
    DisplayMetrics displaymetrics = new DisplayMetrics();  getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
    int screenHeight = displaymetrics.heightPixels; 
    bottomSheetBehavior.setPeekHeight(screenHeight); 

    if (params.getBehavior() instanceof BottomSheetBehavior) { 
     ((BottomSheetBehavior)params.getBehavior()).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
    } 

    params.height = screenHeight; 
    parent.setLayoutParams(params); 
} 
+2

hoạt động tốt, công việc tuyệt vời! –

+0

Cảm ơn bạn @LOG_TAG! :) – Vaigunth

+1

Một điều cần lưu ý với giải pháp này là displayMetrics.heightPixels sẽ bao gồm chiều cao của khay thông báo để điều này sẽ cắt bớt phần dưới một chút. Tôi đã sử dụng tính năng này để tính chiều cao của khay thông báo. 'private int getStatusBarHeight() { Hình chữ nhật cắt = new Rect(); Window window = getActivity(). GetWindow(); window.getDecorView(). GetWindowVisibleDisplayFrame (hình chữ nhật); trả lại hình chữ nhật.top; } ' –

0

Đây là đơn giản nhất và làm việc cho tôi, chỉ cần mở rộng BottomSheetDialog và thiết lập BottomSheetBehavior để BottomSheetBehavior.STATE_EXPANDED

ít hack là bố trí tên android.support.design.R.id.design_bottom_sheet được lấy từ thư viện thiết kế hỗ trợ android

class BottomSheetDialogExpanded(context: Context) : BottomSheetDialog(context) { 

    private lateinit var mBehavior: BottomSheetBehavior<FrameLayout> 

    override fun setContentView(view: View) { 
     super.setContentView(view) 
     val bottomSheet = window.decorView.findViewById<View>(android.support.design.R.id.design_bottom_sheet) as FrameLayout 
     mBehavior = BottomSheetBehavior.from(bottomSheet) 
     mBehavior.state = BottomSheetBehavior.STATE_EXPANDED 
    } 

    override fun onStart() { 
     super.onStart() 
     mBehavior.state = BottomSheetBehavior.STATE_EXPANDED 
    } 
} 
Các vấn đề liên quan