2016-05-08 26 views
6

Chciałbym wyświetlić okno dialogowe dolnego arkusza o mniejszej szerokości niż szerokość ekranu.BottomSheetDialog z przezroczystym tłem

Na przykład opcja na akcje z Muzyki Google Play na Nexusie 9.

the Share option from Google Play Music on a Nexus 9

Czy wiesz jak to osiągnąć?

Na razie udało mi się zmniejszyć szerokość zawartości arkusza, ale tło nadal ma szerokość ekranu i wyświetla białe tło.

Niektóre kodu:

build.gradle

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

główną działalność

@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

proszę podzielić się kod :) –

+0

Jaka wersja Biblioteki projektu jesteś za pomocą? Czy próbowałeś tego z najnowszym (23.3.0)? – ianhanniballake

+0

Myślę, że po prostu trzeba uczynić aktywność nadrzędną przejrzystą. Zobacz http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android – Madushan

Odpowiedz

3

Więc zorientowali się 2 rozwiązania.

najlepszy:

Załóż działalność z przezroczystym tłem dla dolnej blachy. Zaimplementuj własny układ z układem koordynatora i dolnym arkuszem. Ustaw żądany margines. Ustaw żądaną zawartość.

Nie testowano jeszcze.

Lazy jeden:

Rozszerza BottomSheetDialogFragment w onActivityCreated dodatku:

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

To jest odpowiedź: 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. 

a także zmienić android:layout_width="100dp" in nestedScroolView do android:layout_width="match_parent"

0

bit hack, ale działa do tworzenia tła przejrzyste. Oczywiście możesz zastąpić "przezroczysty" dowolnym kolorem.

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

To najprostsze rozwiązanie dla ustawionej przezroczystym tłem BottomSheetDialogFragment

((Widok) contentView.getParent()). SetBackgroundColor (getResources(). GetColor (android.R.color.przezroczysty));

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

ten pracował dla mnie podczas korzystania BottomSheetDialogFragment:

public class CustomDialogFragment extends BottomSheetDialogFragment { 

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

    ... 
} 

dodaj to również do 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> 
Powiązane problemy