gpt4 book ai didi

android - bundle 中实现 Material 设计的问题

转载 作者:行者123 更新时间:2023-12-02 13:33:52 28 4
gpt4 key购买 nike

我试图做一个底纸;该代码似乎正确,但是我在build.gradle中遇到问题。尝试添加implementation "com.google.android.material:material:$material_version"时遇到麻烦。我收到错误
“无法获取未知属性'material_version' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
任何援助将不胜感激

build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
applicationId "com.example.bottom_sheet"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "com.google.android.material:material:$material_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

BottomSheetActivity.kt
class BottomSheetActivity : AppCompatActivity() {

private lateinit var standardBottomSheetBehavior: BottomSheetBehavior<View>

private val startColor = Color.parseColor("#00FFFFFF")
private val endColor = Color.parseColor("#FFFFFFFF")
private val textColor = Color.parseColor("#FF000000")

private var modalDismissWithAnimation = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom_sheet)
setupButtons()
setupStandardBottomSheet()
//animateStandardBottomSheetStates()
}

private fun setupButtons() {
standardBottomSheetButton.setOnClickListener {
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}


}

private fun setupStandardBottomSheet() {
standardBottomSheetBehavior = BottomSheetBehavior.from(standardBottomSheet)
val bottomSheetCallback = object : BottomSheetBehavior.BottomSheetCallback() {

override fun onStateChanged(bottomSheet: View, newState: Int) {
textView.text = when (newState) {
BottomSheetBehavior.STATE_EXPANDED -> "STATE_EXPANDED"
BottomSheetBehavior.STATE_COLLAPSED -> "STATE_COLLAPSED"
BottomSheetBehavior.STATE_DRAGGING -> "STATE_DRAGGING"
BottomSheetBehavior.STATE_HALF_EXPANDED -> "STATE_HALF_EXPANDED"
BottomSheetBehavior.STATE_HIDDEN -> "STATE_HIDDEN"
BottomSheetBehavior.STATE_SETTLING -> "STATE_SETTLING"
else -> null
}
}

override fun onSlide(bottomSheet: View, slideOffset: Float) {
val fraction = (slideOffset + 1f) / 2f
val color = ArgbEvaluatorCompat.getInstance().evaluate(fraction, startColor, endColor)
slideView.setBackgroundColor(color)
}
}
standardBottomSheetBehavior.addBottomSheetCallback(bottomSheetCallback)
standardBottomSheetBehavior.saveFlags = BottomSheetBehavior.SAVE_ALL
textView.setTextColor(textColor)
}

private fun showModalBottomSheet() {
val modalBottomSheet = ModalBottomSheet.newInstance(modalDismissWithAnimation)
modalBottomSheet.show(supportFragmentManager, ModalBottomSheet.TAG)
}

private fun animateStandardBottomSheetStates() {
standardBottomSheet.postDelayed({
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}, 1000L)
standardBottomSheet.postDelayed({
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_COLLAPSED
}, 2000L)
standardBottomSheet.postDelayed({
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
}, 3000L)
standardBottomSheet.postDelayed({
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}, 4000L)
standardBottomSheet.postDelayed({
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN
}, 5000L)
}
}

ModalBottomSheet.kt
class ModalBottomSheet : BottomSheetDialogFragment() {

private var dismissWithAnimation = false

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(R.layout.modal_bottom_sheet, container, false)

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
dismissWithAnimation = arguments?.getBoolean(ARG_DISMISS_WITH_ANIMATION) ?: false
(requireDialog() as BottomSheetDialog).dismissWithAnimation = dismissWithAnimation
}

companion object {
const val TAG = "ModalBottomSheet"
private const val ARG_DISMISS_WITH_ANIMATION = "dismiss_with_animation"
fun newInstance(dismissWithAnimation: Boolean): ModalBottomSheet {
val modalBottomSheet = ModalBottomSheet()
modalBottomSheet.arguments = bundleOf(ARG_DISMISS_WITH_ANIMATION to dismissWithAnimation)
return modalBottomSheet
}
}
}

最佳答案

您需要更改此行:

implementation "com.google.android.material:material:$material_version"

对此:
implementation 'com.google.android.material:material:1.2.0-alpha03'

或将 $material_version参数添加到您的build.gradle文件中

关于android - bundle 中实现 Material 设计的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59857069/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com