2016-09-23 76 views
1

我试图在ButtonSheetDialogFragment布局中设置边距,但它不起作用。我试图从布局设置margin和编程,但其具有相同的结果 这是我的XML文件layout_bts_item.xml如何在ButtomSheetDialogFragment Android中设置左右边距?

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:background="#00000000"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </LinearLayout> 
</FrameLayout> 

这是我的Java代码

 public class ButtomSheetFragment 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(getContext(),   R.layout.layout_bts_item, null); 
      dialog.setContentView(contentView); 
      CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams(); 
      CoordinatorLayout.Behavior behavior = params.getBehavior(); 
      if (behavior != null && behavior instanceof BottomSheetBehavior) { 
       ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback); 
       int height = LayoutUtils.getScreenHeight(getActivity()); 
       double desiredHeight = (0.85 * height); 
       ((BottomSheetBehavior) behavior).setPeekHeight((int) desiredHeight); 
       contentView.getLayoutParams().height = (int) desiredHeight; 
       ((FrameLayout.LayoutParams) contentView.getLayoutParams()).leftMargin = 100; 
      } 
     } 
    } 
+0

什么是ButtonSheetDialogFragment? –

+0

另外,你尝试了什么?发生了什么? –

+0

请检查此ButtonSheetDialogFragment https://developer.android.com/reference/android/support/design/widget/BottomSheetDialogFragment.html –

回答

0

呼叫requestLayout()一旦你完成设置保证金。

在你的情况,像

contentView.requestLayout(); 

添加左边距之后。

+0

不工作:( –

相关问题