Android Studio Kotlin日期选择器

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

Android Studio Kotlin DatePicker

问题

如何在不从头开始创建自定义DatePicker的情况下使其四个角变为圆角?我是否可以以某种方式将背景形状分配给XML?

我尝试在主题中添加样式,在那里我注册了android:background corners_20dp.xml,但那并没有奏效。

我真的需要帮助。提前感谢您的想法和实现方法!

英文:

How can I round the corners of a DatePicker without creating a custom DatePicker from scratch? Can I somehow assign a background shape to xml?

I tried to add styles in themes where I registered android:background corners_20dp.xml But it didn't work out that way.

I really need help. Thank you in advance for the ideas and ways of implementation!

答案1

得分: 0

我有解决这个问题的方法。我将逐步解释如何操作。

首先,在你的drawable文件夹中创建一个新的“Drawable资源文件”,你可以右键单击,然后命名为“rounded_corner”,点击“确定”即可创建文件。

在你的drawable XML文件中写入以下内容:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="28dp" />
    <solid android:color="?colorBackgroundFloating" />
</shape>

现在,我们可以为DatePicker设置圆角,使用以下代码:

val datePicker = DatePickerDialog(
    requireContext(),
    { _, y, m, d ->
        // 这是监听器
    },
    cal.get(Calendar.YEAR),
    cal.get(Calendar.MONTH),
    cal.get(Calendar.DAY_OF_MONTH)
)

// 这里为DatePicker设置圆角
datePicker.window?.setBackgroundDrawable(
    ContextCompat.getDrawable(
        requireContext(),
        R.drawable.rounded_corner
    )
)

datePicker.show()

结果:
Android Studio Kotlin日期选择器

英文:

I have solution for this problem. I'm explain how can you do step by step.

First, create new Drawable Resource File, you can do this with right click in your drawable folder after that you can give name rounded_corner click OK file will be created.

Write this in your drawable 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;&gt;
    &lt;corners android:radius=&quot;28dp&quot; /&gt;
    &lt;solid android:color=&quot;?colorBackgroundFloating&quot; /&gt;
&lt;/shape&gt;

Now, we can give this rounded corner for DatePicker, for this we use that code:

val datePicker = DatePickerDialog(
    requireContext(),
    { _, y, m, d -&gt;
        // it&#39;s listener
    },
    cal.get(Calendar.YEAR),
    cal.get(Calendar.MONTH),
    cal.get(Calendar.DAY_OF_MONTH)
)

// Here is giving rounded corner for DatePicker
datePicker.window?.setBackgroundDrawable(
    ContextCompat.getDrawable(
        requireContext(),
        R.drawable.rounded_corner
    )
)

datePicker.show()

Result:
Android Studio Kotlin日期选择器

huangapple
  • 本文由 发表于 2023年5月17日 20:29:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272126.html
匿名

发表评论

匿名网友

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

确定