英文:
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()
英文:
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:
<?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>
Now, we can give this rounded corner for DatePicker, for this we use that code:
val datePicker = DatePickerDialog(
requireContext(),
{ _, y, m, d ->
// it'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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论