【问题标题】:Adding margin to BottomSheetDialogFragment向 BottomSheetDialogFragment 添加边距
【发布时间】:2020-03-14 03:40:01
【问题描述】:

我尝试向我的 BottomSheetDialogFragment 添加边距,但它对边距没有任何作用。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_margin="16dp">

    <TextView
        android:id="@+id/alertdialog_fragmail_newmessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1"
        android:textStyle="bold"
        android:padding="16dp"
        android:textColor="@color/colorBlackFont"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:drawablePadding="16dp"/>


  //More Textviews

</RelativeLayout>

编辑:___________________________________________________

我已尝试将 XML 更改为下面的答案,但它仍然没有为我的 bottomsheetdialogfragment 创建边距。

Bottom sheet 对话框片段类的代码:

public class FragMailMoreDialog extends BottomSheetDialogFragment {

    private static final String TAG = "FragMailMoreDialog";

    private Context mContext;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.alertdialog_layout_fragmailmore, container, false);
        ButterKnife.bind(this, view);


        return view;
    }
}

给底页充气的代码:

private void inflateMoreDialog(){
        FragMailMoreDialog moreDialog = new FragMailMoreDialog();
        if (getFragmentManager() != null) {
            moreDialog.show(getFragmentManager(), "FRAGMAIL_MORE_DIALOG");
        }
    }

【问题讨论】:

  • 请附上完整代码
  • 您是否期望所有边都有边距,有点像浮动窗口?还是只在左右两侧? This answer 会左右操作,但您可能需要更新主题和样式 parents - 如果您使用的是较新的库 - 您必须在 ButtomSheetFragment 中覆盖 onCreateDialog() 以返回一个BottomSheetDialog 构造,如那里所示。不过,没有setContentView()show() 电话。
  • 是的,我正在尝试像浮动窗口一样在各个方面获得边距

标签: android android-bottomsheetdialog


【解决方案1】:

有点老套的解决方案:

我将布局包裹在另一个 RelativeLayout 中,并使该布局的背景透明。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorWhite"
        android:layout_margin="16dp">

    <TextView
        android:id="@+id/alertdialog_fragmail_newmessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test 1"
        android:textStyle="bold"
        android:padding="16dp"
        android:textColor="@color/colorBlackFont"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:drawablePadding="16dp"
        android:background="?attr/selectableItemBackground"/>
    </RelativeLayout>
</RelativeLayout>

在 BottomSheetDialogFragment 中,您需要覆盖 setupDialog

@Override
    public void setupDialog(Dialog dialog, int style) {
        View contentView = View.inflate(getContext(), R.layout.alertdialog_layout_fragmailmore, null);
        dialog.setContentView(contentView);
        ((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }

感谢此人:

https://stackoverflow.com/a/55219784/11110509

【讨论】:

    【解决方案2】:

    覆盖setupDialog() 对我不起作用,而且我需要使用onCreateView() 来扩充对话框布局。因此,这可以在自定义的BottomSheetDialogFragment 类中以编程方式解决:

    • 创建无背景样式并将其设置为onCreateDialoggetTheme() 中的对话框
    <style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="android:windowBackground">@null</item>
    </style>
    
    • 使用对话框布局根目录的layoutParams 设置边距

    Java:

    public class FragMailMoreDialog extends BottomSheetDialogFragment {
    
        //.... Omitted code
    
            
        @Override
        public int getTheme() {
           // Step 1
           return R.style.NoBackgroundDialogTheme;
        }
    
    
        @Override
        public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            
            // Step 2
            addMargin(view);
    
        }
    
        private void addMargin(View view) {
            FrameLayout.LayoutParams layoutParams =
                    (FrameLayout.LayoutParams) view.getLayoutParams();
            int margin_16dp = dpToPx(16);
            layoutParams.setMargins(margin_16dp, margin_16dp, margin_16dp, margin_16dp);
            view.setLayoutParams(layoutParams);
            view.requestLayout();
        }
    
        private int dpToPx(int dp) {
            Resources r = getResources();
            int px = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP,
                    dp,
                    r.getDisplayMetrics()
            );
            return px;
        }
    
    }
    

    科特林:

    class FragMailMoreDialog() : BottomSheetDialogFragment() {
    
        override fun getTheme(): Int {
            return R.style.NoBackgroundDialogTheme
        }
    
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
    
            val view: View = View.inflate(context, R.layout.my_fragment_bottomsheet, null)
            return view
        }
    
    
        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            addMargin(view)
        }
    
        private fun addMargin(view: View) {
            val layoutParams: FrameLayout.LayoutParams =
                view.layoutParams as FrameLayout.LayoutParams
            val margin_16dp = 16.toPx().toInt()
            layoutParams.setMargins(margin_16dp, margin_16dp, margin_16dp, margin_16dp)
    
            view.layoutParams = layoutParams
        }
    
    
        fun Number.toPx() = TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            this.toFloat(),
            Resources.getSystem().displayMetrics
        )
    
    }
    

    【讨论】:

      【解决方案3】:

      不知道为什么要为 BottomSheetDialogFragment 设置边距。它是一个 DialogFragment,显示在您的 Activity/Fragment 之上。为其添加边距不会做任何事情。 如果您需要的是 TextView (Test1) 的顶部填充和 TextView (Test2) 的底部填充,那么您应该将 padding_top 添加到 Test1 并将 padding_bottom 添加到 Test2 ,例如:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_margin="16dp">
      
          <TextView
              android:id="@+id/alertdialog_fragmail_newmessage"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Test 1"
              android:textStyle="bold"
              android:paddingTop="32dp"
              android:paddingStart="16dp"
              android:paddingEnd="16dp"
              android:paddingBottom="16dp"
              android:textColor="@color/colorBlackFont"
              android:layout_alignParentTop="true"
              android:gravity="center_vertical"
              android:drawablePadding="16dp"/>
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        我为 Constraint 子元素添加了边距并让父 Constraint 背景透明Bottom Sheet layout Image

        <androidx.constraintlayout.widget.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:background="#99000000"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <androidx.constraintlayout.widget.ConstraintLayout
                style="@style/BottomSheetDialogStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_medium"
                android:layout_marginEnd="@dimen/margin_medium"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent">
        
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/iv_add_post_ic"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_medium"
                    android:layout_marginTop="@dimen/margin_form"
                    android:layout_marginBottom="@dimen/margin_small"
                    android:src="@drawable/ic_add_post"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
        
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tv_add_post"
                    style="@style/Widget.nejmo.Text.Medium"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/margin_form"
                    android:layout_marginHorizontal="@dimen/margin_small"
                    android:layout_marginStart="@dimen/margin_small"
                    android:layout_marginBottom="@dimen/margin_small"
                    android:text="@string/option_add_post_txt"
                    android:textColor="@color/dusk"
                    app:layout_constraintBottom_toBottomOf="@+id/iv_add_post_ic"
                    app:layout_constraintStart_toEndOf="@+id/iv_add_post_ic"
                    app:layout_constraintTop_toTopOf="@+id/iv_add_post_ic" />
        
        
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/iv_add_question_ic"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/margin_medium"
                    android:layout_marginTop="@dimen/margin_medium"
                    android:layout_marginBottom="@dimen/margin_medium"
                    android:src="@drawable/ic_add_question"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/iv_add_post_ic" />
        
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tv_add_question"
                    style="@style/Widget.nejmo.Text.Medium"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/margin_form"
                    android:layout_marginHorizontal="@dimen/margin_small"
                    android:layout_marginStart="@dimen/margin_small"
                    android:layout_marginTop="@dimen/margin_medium"
                    android:layout_marginBottom="@dimen/margin_medium"
                    android:text="@string/option_add_question_txt"
                    android:textColor="@color/dusk"
                    app:layout_constraintBottom_toBottomOf="@+id/iv_add_question_ic"
                    app:layout_constraintStart_toEndOf="@+id/iv_add_question_ic"
                    app:layout_constraintTop_toTopOf="@+id/iv_add_question_ic" />
        
            </androidx.constraintlayout.widget.ConstraintLayout>
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          【解决方案5】:

          100% 有效

          覆盖 onCreateDialog() 返回您自己的对话框

             override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
            
                  return Dialog(requireActivity()).apply {
                      setContentView(viewBinding.root)
                      window!!.setBackgroundDrawableResource(R.drawable.dialog_background)
                      window!!.setGravity(Gravity.BOTTOM)
                      window!!.setLayout(500.toPx(),500.toPx())
                  }
            
          }
          

          【讨论】:

            【解决方案6】:

            类似于@Zain 的回答:

            override fun onCreateView(...) {
                ...
            }
            
            override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                super.onViewCreated(view, savedInstanceState)
            
                // FrameLayout.LayoutParams, CoordinatorLayout.LayoutParams or other container in your root layout.
                ((view.parent as View).layoutParams as FrameLayout.LayoutParams).topMargin = 100
            }
            

            【讨论】:

              【解决方案7】:

              您可以在stiles.xml 中进行操作。请注意,要使bottom_margin 起作用,您实际上需要添加一个负数top_margin(请参阅-10dp),这会将所有内容上移10dp。

              <style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
                  <item name="bottomSheetStyle">@style/AppModalStyle</item>
                  <item name="android:windowIsTranslucent">false</item>
                  <item name="android:windowContentOverlay">@null</item>
                  <item name="android:backgroundDimAmount">0.5</item>
                  <item name="android:windowFrame">@null</item>
                  <item name="android:windowIsFloating">true</item>
              </style>
              
              <style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
                  <item name="android:background">@drawable/bottom_menu_back</item>
                  <item name="android:layout_marginStart">10dp</item>
                  <item name="android:layout_marginEnd">10dp</item>
                  <item name="android:layout_marginTop">-10dp</item>
              </style>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2022-01-20
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-10-09
                • 2015-08-14
                • 2015-06-25
                相关资源
                最近更新 更多