API <21 下 BottomSheetDialogFragment 的圆角不起作用

3
我正在使用 这个解决方案 来为 BottomSheetDialogFragment 中的对话框设置圆角,它在 API 21 及更高版本中运行良好。

enter image description here

但是在Api < 21中,它会移除背景并且圆角背景也会消失。 enter image description here 如何在API < 21中使背景变成圆角? 如果无法更改背景,请帮我更改背景颜色。

你可以在底部弹出对话框中使用 CardView,从而实现圆角效果。@Morteza - Brahma Datta
@g.brahmaDatta 怎样移除 BottomSheetDialogFragment 的背景? - Morteza Rastgoo
我按照你的指示做了,它在KitKat版本的手机上也能正常工作。@Morteza - Brahma Datta
@VIN 是一个 SVG 图像。 - Morteza Rastgoo
@ManoharPerepa 这是一个带有SVG可绘制的imageView。 - Morteza Rastgoo
显示剩余9条评论
3个回答

8

Morteza,我编写了以下代码使得 BottomSheetDialog Fragment 对话框拥有圆角,并已在 KitKat 版本的手机上进行了测试。

Bottom Sheet Dialog 类的代码

public class MyBottomSheetDialog extends BottomSheetDialogFragment {

String string;

static MyBottomSheetDialog newInstance(String string) {
    MyBottomSheetDialog f = new MyBottomSheetDialog();
    Bundle args = new Bundle();
    args.putString("string", string);
    f.setArguments(args);
    return f;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    string = getArguments().getString("string");
    //bottom sheet round corners can be obtained but the while background appears to remove that we need to add this.
    setStyle(DialogFragment.STYLE_NO_FRAME,0);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.bottom_sheet_modal, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);

    //dialog cancel when touches outside (Optional)
    getDialog().setCanceledOnTouchOutside(true);
    return v;
}}

bottom_sheet_modal.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
//adding background from drawable
android:background="@drawable/rounded_dialog">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center"
    android:gravity="center"
    android:weightSum="10"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp">


    <Button
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"
        android:text="Buy"
        />

    <Button
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"
        android:text="sell"
        />

</LinearLayout>
</LinearLayout>

rounded_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#444343"/>
<corners android:topLeftRadius="16dp"
    android:topRightRadius="16dp"/>

</shape>

MainActivity.java

public class MainActivity extends AppCompatActivity {

BottomSheetDialogFragment bottomSheetDialogFragment;
Button button;
LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bottomSheetDialogFragment = MyBottomSheetDialog.newInstance("Bottom Sheet Dialog");
    button = findViewById(R.id.button);



    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            bottomSheetDialogFragment.show(getSupportFragmentManager(),bottomSheetDialogFragment.getTag());
        }
    });

}
}

尝试这个,然后让我知道 @Morteza。编码愉快。

1
将此代码添加到片段中后,单击片段外部时无法关闭该片段。 - OhhhThatVarun

3
创建一个自定义的可绘制文件rounded_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white"/>
    <corners android:topLeftRadius="16dp"
        android:topRightRadius="16dp"/>

</shape>

        view!!.getViewTreeObserver().addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                if (Build.VERSION.SDK_INT < 16) {
                    view!!.getViewTreeObserver().removeGlobalOnLayoutListener(this)
                } else {
                    view!!.getViewTreeObserver().removeOnGlobalLayoutListener(this)
                }
                val dialog = dialog as BottomSheetDialog?
                val bottomSheet =
                    dialog!!.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout?

                //Change background Image for all android versions below Api < 21
                bottomSheet!!.setBackgroundResource(R.drawable.rounded_dialog)
            }
        })


0
最简单、最干净的解决方案,对我而言有效的方法是,在我的片段类的onViewCreated(View view, Bundle savedInstanceState)方法中放置以下3行代码:
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    View bottomSheet = (View) view.getParent();
    bottomSheet.setBackgroundTintMode(PorterDuff.Mode.CLEAR);
    bottomSheet.setBackgroundTintList(ColorStateList.valueOf(Color.TRANSPARENT));
    bottomSheet.setBackgroundColor(Color.TRANSPARENT);
}

这将允许您的自定义带圆角的可绘制对象在设置为片段布局的顶层视图的背景后正确显示。

本质上,这将覆盖默认的BottomSheetFragment属性,包括颜色、tintMode和tintList。

使用此方法,无需处理样式资源。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接