Jectpack组件
Navigation
问题描述:BottomNavigationView控件添加到布局文件中导致预览无法预览视图
问题原因:Material Design 组件库的版本过高。
解决方案:
dependencies {
implementation 'com.google.android.material:material:1.4.0'
}
NavDirections
问题描述:在使用Navigation组件进行导航时,通过NavDirection将数据传递给另一个界面。但是在构建项目时,使用Safe Args插件自动生成的Args类报错,无法获取传递的数据。
问题原因:Safe Args插件版本和Navigation组件版本不一致
解决方案:
//项目级别下的build.gradle
buildscript {
ext{
navigation_version = '2.5.1'
}
dependencies {
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:${navigation_version}"
}
}
//应用级别下的build.gradle
dependencies {
//navigation
implementation "androidx.navigation:navigation-fragment-ktx:${navigation_version}"
implementation "androidx.navigation:navigation-ui-ktx:${navigation_version}"
}
开源项目(github)
开源组件
ShimmerRecyclerViewX
问题描述:gradle中导入依赖失败
问题原因:可能是需要给gradle添加代理
解决方案:
这个需要添加到setting.gradle中的dependencyResolutionManagement里的repositories
repositories {
maven { url "https://jitpack.io" }
}
这个添加到build.gradle中
implementation 'com.github.mike14u:shimmer-recyclerview-x:1.0.4'
弃用函数
setHasOptionMenu
- Activity
/**
* Using the addMenuProvider() API directly in your Activity
**/
class ExampleActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Add menu items without overriding methods in the Activity
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items here
menuInflater.inflate(R.menu.example_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return true
}
})
}
}
- Fragment
/**
* Using the addMenuProvider() API in a Fragment
**/
class ExampleFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// The usage of an interface lets you inject your own implementation
val menuHost: MenuHost = requireActivity()
// Add menu items without using the Fragment Menu APIs
// Note how we can tie the MenuProvider to the viewLifecycleOwner
// and an optional Lifecycle.State (here, RESUMED) to indicate when
// the menu should be visible
menuHost.addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
// Add menu items here
menuInflater.inflate(R.menu.example_menu, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
// Handle the menu selection
return true
}
}, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
build.gradle(app)
dependencies {
val activity_version = "1.5.1"
// Kotlin
implementation("androidx.activity:activity-ktx:$activity_version")
}
安卓资源中单复数Plurals问题
问题描述:plurals在中文设备上无法起作用
问题原因:应用程序本地化。如果在应用程序中使用英语作为区域设置,则这些代码应该有效。否则可能不起作用。中文没有单复数之分
解决方案:无
使用方法:
Android 中的数量字符串。通过声明 plurals
资源,您可以根据具体数量指定要使用的不同字符串资源,例如采用单数或复数形式。
- 在
strings.xml
文件中添加一个cupcakes
复数资源。
<plurals name="cupcakes">
<item quantity="one">%d cupcake</item>
<item quantity="other">%d cupcakes</item>
</plurals>
在单数情况 (quantity="one"
) 下,将使用单数形式的字符串。在所有其他情况 (quantity="other"
) 下,将使用复数形式的字符串。请注意,与需要字符串参数的 %s
不同,%d
需要的是整数参数,您将在格式化字符串时传入该整数参数。
在您的 Kotlin 代码中,调用
getQuantityString(R.plurals.cupcakes, 1, 1)将返回字符串
1 cupcake
getQuantityString(R.plurals.cupcakes, 6, 6)将返回字符串
6 cupcakes
getQuantityString(R.plurals.cupcakes, 0, 0)将返回字符串
0 cupcakes
注意:调用
getQuantityString()
时,您需要传入两次数量,因为第一个数量参数将用于选择正确的复数形式字符串。第二个数量参数用于实际字符串资源的%d
占位符。
Room版本问题
问题描述:
room版本在4.0以下,如果在dao接口中的方法前加suspend修饰符出现报错错误: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
解决方案:
- room版本修改到4.0以上
- 去掉suspend修饰符,但是耗时操作依然要放在协程中执行,使用IO调度程序