如何使底部对话框的角变圆?

huangapple go评论77阅读模式
英文:

How can I make the corners of my bottom sheet dialog rounded?

问题

我试图使我的BottomSheetDialog的顶部角变成圆角,但是在网上找到的任何方法都没有成功。我希望它的样子是这样的:

如何使底部对话框的角变圆?

无论我尝试了什么,我总是得到这样的结果:

如何使底部对话框的角变圆?

我已经尝试了这里的方法,以及使用shapeAppearanceLargeComponent(我现在正在使用的方法)。

这是我的代码:

styles.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
    <!-- 自定义您的主题在这里。 -->
    ...
    <item name="shapeAppearanceLargeComponent">@style/CustomShapeAppearanceBottomSheetDialog</item>
</style>

<style name="CustomShapeAppearanceBottomSheetDialog" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
</style>

BottomNavMenuFragment:

public class BottomNavMenuFragment extends BottomSheetDialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_bottom_nav_drawer, container, false);
    }
}

这是我显示该片段的方式:

BottomNavMenuFragment navFragment = new BottomNavMenuFragment();
navFragment.show(getSupportFragmentManager(), navFragment.getTag());

无论我做什么,似乎都不起作用。有人能指点我正确的方向吗?

英文:

I'm trying to make the top corners of my BottomSheetDialog rounded, but I haven't had any luck with anything online. This is what I would like for it to look like:

如何使底部对话框的角变圆?

No matter what I've tried, I keep getting this:

如何使底部对话框的角变圆?

I've tried the method here and using shapeAppearanceLargeComponent (what I'm using now).

Here is my code:

styles.xml

 &lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.Light.DarkActionBar&quot;&gt;
    &lt;!-- Customize your theme here. --&gt;
    ...
    &lt;item name=&quot;shapeAppearanceLargeComponent&quot;&gt;@style/CustomShapeAppearanceBottomSheetDialog&lt;/item&gt;
&lt;/style&gt;

&lt;style name=&quot;CustomShapeAppearanceBottomSheetDialog&quot; parent=&quot;&quot;&gt;
    &lt;item name=&quot;cornerFamily&quot;&gt;rounded&lt;/item&gt;
    &lt;item name=&quot;cornerSizeTopRight&quot;&gt;16dp&lt;/item&gt;
    &lt;item name=&quot;cornerSizeTopLeft&quot;&gt;16dp&lt;/item&gt;
&lt;/style&gt;

BottomNavMenuFragment:

public class BottomNavMenuFragment extends BottomSheetDialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_bottom_nav_drawer, container, false);
    }
}

And this is how I'm showing the fragment:

BottomNavMenuFragment navFragment = new BottomNavMenuFragment();
navFragment.show(getSupportFragmentManager(), navFragment.getTag());

Nothing I seem to do works. Could someone point me in the right direction?

答案1

得分: 11

跟随以下步骤以获得具有顶部圆角的底部对话框:

步骤1:drawable文件夹内创建名为rounded_top_corners.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="8dp"
        android:topRightRadius="8dp"/>
</shape>

步骤2:styles.xml中创建以下样式

<style name="BottomSheetDialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/bottomSheetBackground</item>
</style>

<style name="bottomSheetBackground" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rounded_top_corners</item>
</style>

步骤3:BottomNavMenuFragment类中添加样式

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

就是这样,样式将被应用于底部对话框。

英文:

Follow the below steps to attain top rounded corner bottomsheet:

Step 1: Create drawable rounded_top_corners.xml file inside drawable folder

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:shape=&quot;rectangle&quot;&gt;
    &lt;solid android:color=&quot;@android:color/white&quot;/&gt;
    &lt;corners android:topLeftRadius=&quot;8dp&quot;
        android:topRightRadius=&quot;8dp&quot;/&gt;
&lt;/shape&gt;

Step 2: Create below styles in styles.xml

 &lt;style name=&quot;BottomSheetDialogStyle&quot; parent=&quot;Theme.Design.Light.BottomSheetDialog&quot;&gt;
        &lt;item name=&quot;bottomSheetStyle&quot;&gt;@style/bottomSheetBackground&lt;/item&gt;
    &lt;/style&gt;

    &lt;style name=&quot;bottomSheetBackground&quot; parent=&quot;Widget.Design.BottomSheet.Modal&quot;&gt;
        &lt;item name=&quot;android:background&quot;&gt;@drawable/rounded_top_corners&lt;/item&gt;
    &lt;/style&gt;

Step 3: Add style in BottomNavMenuFragment class

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

That's it, the style will be applied to bottomsheet.

答案2

得分: 3

经过尝试了一些人们提供的可能解决方案后,我发现我的代码运行正常,但是我的NavigationView的角落遮挡了抽屉的圆角。添加了一些内边距后,圆角现在正确显示。

英文:

After messing around with the possible solutions people posted, I figured out that my code was working fine, but the corners of my NavigationView were obscuring the rounded corners of the drawer. After adding some padding, the rounded corners are displaying correctly.

答案3

得分: 0

你可以在drawable文件夹中创建如下形状:

drawable/rounded_corners.xml

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

然后,对于底部弹出窗口的布局,你可以将这个drawable作为背景属性添加:

android:background="@drawable/rounded_corners"
英文:

You can create following shape in your drawable:
>drawable/rounded_corners.xml

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:shape=&quot;rectangle&quot;&gt;
    &lt;solid android:color=&quot;#fff&quot;/&gt;
    &lt;corners 
        android:topLeftRadius=&quot;20dp&quot;
        android:topRightRadius=&quot;20dp&quot;/&gt;
&lt;/shape&gt;

<!-- end snippet -->

Then for the layout for your bottom sheet you can add this drawable as a background property.

android:background=&quot;@drawable/rounded_corners&quot;

答案4

得分: 0

以下是您提供的内容的翻译:

创建一个可绘制对象并在 setOnShowListener 内部设置给 bottomsheet,如下所示。(其中 R.drawable.bottom_sheet_bkg 是我的背景 XML 文件)。

val dialogBinding = MyBottomSheetDialogBinding.inflate(layoutInflater)
val bottomSheet = BottomSheetDialog(requireContext())
bottomSheet.setContentView(dialogBinding.root)
bottomSheet.setOnShowListener { dia ->
    val bottomSheetDialog = dia as BottomSheetDialog
    val bottomSheetInternal: FrameLayout =
        bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
    bottomSheetInternal.setBackgroundResource(R.drawable.bottom_sheet_bkg)
}
bottomSheet.setCancelable(true)
bottomSheet.show()

XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/white" />
    <corners
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
</shape>
英文:

Create a drawable and set it for the bottomsheet inside the setOnShowListener as below.(where R.drawable.bottom_sheet_bkg is my background xml file).

        val dialogBinding = MyBottomSheetDialogBinding.inflate(layoutInflater)
        val bottomSheet = BottomSheetDialog(requireContext())
        bottomSheet.setContentView(dialogBinding.root)
        bottomSheet.setOnShowListener { dia -&gt;
            val bottomSheetDialog = dia as BottomSheetDialog
            val bottomSheetInternal: FrameLayout =
                bottomSheetDialog.findViewById(com.google.android.material.R.id.design_bottom_sheet)!!
            bottomSheetInternal.setBackgroundResource(R.drawable.bottom_sheet_bkg)
        }
        bottomSheet.setCancelable(true)
        bottomSheet.show()

XML File:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;shape xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:shape=&quot;rectangle&quot;&gt;
    &lt;solid android:color=&quot;@color/white&quot; /&gt;
    &lt;corners
        android:topLeftRadius=&quot;5dp&quot;
        android:topRightRadius=&quot;5dp&quot; /&gt;
&lt;/shape&gt;

huangapple
  • 本文由 发表于 2020年5月30日 16:52:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/62100100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定