android 向BottomSheetDialogFragment添加边距

0dxa2lsx  于 2023-04-28  发布在  Android
关注(0)|答案(7)|浏览(214)

我已经尝试在我的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创建边距。
底部工作表对话框片段类的代码:

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");
        }
    }
q35jwt9p

q35jwt9p1#

一个简单的解决方案:
我将我的布局 Package 在另一个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

unhi4e5o

unhi4e5o2#

你可以在stiles.xml中做。请注意,要让bottom_margin工作,你实际上需要添加一个负的marginTop(参见-10dp),这将使所有内容都向上移动10 dp。

<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>
7qhs6swi

7qhs6swi3#

覆盖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;
    }

}

Kotlin:

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
    )

}
wpcxdonn

wpcxdonn4#

不确定为什么要为BottomSheetDialogFragment添加边距。它是一个显示在Activity/Fragment顶部的DialogFragment。向其添加边距不会有任何作用。如果您需要的是TextView的顶部填充(Test1)和TextView的底部填充(Test 2),那么您应该向Test1添加padding_top,向Test 2添加padding_bottom,如下所示:

<?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>
y1aodyip

y1aodyip5#

I为约束子级添加边距,并给予父级约束背景提供透明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>
tuwxkamq

tuwxkamq6#

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())
        }
  
}
ibrsph3r

ibrsph3r7#

类似@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
}

相关问题