2016-05-08 27 views
6

Tôi muốn hiển thị hộp thoại trang tính dưới cùng rộng hơn chiều rộng màn hình.BottomSheetDialog với nền trong suốt

Ví dụ, Chia sẻ tùy chọn từ Google Play Âm nhạc trên Nexus 9.

the Share option from Google Play Music on a Nexus 9

Bạn có biết làm thế nào để đạt được điều này?

Hiện tại, tôi vừa mới làm giảm độ rộng của nội dung trang tính nhưng nền vẫn ở độ rộng màn hình và hiển thị nền trắng.

Một số mã:

build.gradle

compile 'com.android.support:design:23.3.0' 

MainActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 

    mBottomSheetDialog = new BottomSheetDialog(this); 
    mBottomSheetDialog.setContentView(R.layout.sheet_test); 
    mBottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() { 
     @Override 
     public void onDismiss(DialogInterface dialog) { 
      mBottomSheetDialog = null; 
     } 
    }); 
    mBottomSheetDialog.show(); 
} 

sheet_test

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="100dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      style="@style/TextAppearance.AppCompat.Body1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:text="Some Text" 
      android:textColor="@color/colorPrimary" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#ddd" /> 

     <TextView 
      style="@style/TextAppearance.AppCompat.Body1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_margin="16dp" 
      android:text="Some Text" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="#ddd" /> 

    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 
+0

xin chia sẻ một số Mã :) –

+0

phiên bản của Thư viện Thiết kế là gì bạn sử dụng? Bạn đã thử nó với phiên bản mới nhất (23.3.0) chưa? – ianhanniballake

+0

Tôi nghĩ bạn chỉ cần làm cho hoạt động của phụ huynh trong suốt. Tham khảo http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android – Madushan

Trả lời

3

Vì vậy, tôi đã tìm ra 2 giải pháp.

nhất một:

Tạo một hoạt động với nền trong suốt chỉ cho tấm dưới cùng của bạn. Thực hiện bố cục của riêng bạn với bố cục điều phối và trang tính dưới cùng. Đặt lề bạn muốn. Đặt nội dung bạn muốn.

Chưa được thử nghiệm.

Lazy một:

Mở rộng BottomSheetDialogFragment, trong onActivityCreated add:

Resources resources = getResources(); 

    // Set margin for Landscape Mode. Maybe a more elegant solution will be to implements our own bottom sheet with our own margins. 
    if (resources.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     assert getView() != null; 
     View parent = (View) getView().getParent(); 
     CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) parent.getLayoutParams(); 
     layoutParams.setMargins(
       resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_left), // 64dp 
       0, 
       resources.getDimensionPixelSize(R.dimen.bottom_sheet_margin_right), // 64dp 
       0 
     ); 
     parent.setLayoutParams(layoutParams); 
    } 
1

Đây là câu trả lời của bạn: D

View contentView=LayoutInflater.from(getContext()).inflate(R.layout.bs_add_event,null); 
    mBottomSheetDialog.setContentView(contentView); 

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams(); 
    params.setMargins(50,50,50,50); // set margin as your wish. 

và cũng thay đổi android:layout_width="100dp" in nestedScroolView để android:layout_width="match_parent"

0

Bit của một hack nhưng nó hoạt động để làm nền trong suốt. Rõ ràng bạn có thể thay thế 'trong suốt' bằng bất kỳ màu nào bạn muốn.

mBottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent); 
25

Đây là giải pháp đơn giản nhất để thiết lập nền trong suốt của BottomSheetDialogFragment

((Xem) contentView.getParent()). SetBackgroundColor (getResources(). GetColor (android.R.color.trong suốt));

public class ShareOneTouchAlertNewBottom extends BottomSheetDialogFragment { 
    @Override 
    public void setupDialog(Dialog dialog, int style) { 
     super.setupDialog(dialog, style); 
     View contentView = View.inflate(getContext(), R.layout.fragment_bottom_sheet, null); 
     dialog.setContentView(contentView); 

     CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()) 
       .getLayoutParams(); 
     CoordinatorLayout.Behavior behavior = params.getBehavior(); 
     ((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent)); 
    } 
} 
3

này làm việc cho tôi khi sử dụng BottomSheetDialogFragment:

public class CustomDialogFragment extends BottomSheetDialogFragment { 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme); 
    } 

    ... 
} 

Ngoài ra thêm này để bạn styles.xml

<style name="CustomBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog"> 
    <item name="bottomSheetStyle">@style/CustomBottomSheetStyle</item> 
</style> 

<style name="CustomBottomSheetStyle" parent="Widget.Design.BottomSheet.Modal"> 
    <item name="android:background">@android:color/transparent</item> 
</style>