gpt4 book ai didi

java - Unresolved reference : LayoutParams on Kotlin

转载 作者:行者123 更新时间:2023-11-30 05:09:28 30 4
gpt4 key购买 nike

我在将函数从 Java 语言转换为 Kotlin 时遇到问题。我用的是AS 3.2.1

这是我在 Java 上的函数

private class ImageViewFactory implements ViewSwitcher.ViewFactory {
@Override
public View makeView() {
final ImageView imageView = new ImageView(MainActivity.this);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

final LayoutParams lp = new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);

return imageView;
}
}

在 kotlin 中如下所示

private inner class ImageViewFactory : ViewSwitcher.ViewFactory {
override fun makeView(): View {
val imageView = ImageView(this@MainActivity)
imageView.scaleType = ImageView.ScaleType.CENTER_CROP

val lp =
ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
imageView.layoutParams = lp

return imageView
}
}

问题来自 ImageSwitcher.LayoutParams,在 Java 上它运行良好。但不幸的是 Kotlin 不承认这些功能。

这是我的 Java 应用程序 gradle

android {
compileSdkVersion 26
buildToolsVersion '27.0.3'

defaultConfig {
minSdkVersion 19
targetSdkVersion 26

implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation project(':card-slider')

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'

然后从我的 Kotlin Gradle 中吼叫

android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 19
targetSdkVersion 26

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:cardview-v7:26.1.0'

implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.facebook.android:facebook-login:[4,5)'
implementation 'com.ramotion.cardslider:card-slider:0.3.0'

它在 Android Studio 上显示语法错误 Unresolved references : LayoutParams

我不确定上面发生了什么。请让我知道我错过了什么。谢谢

最佳答案

我认为 ImageSwitcher 没有内部类 LayoutParams 所以你不能直接访问它所以只使用他的父布局,在这种情况下是 FrameLayout

为了解决问题,只需更换这一行

val lp = ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)

因为 document

   ↳    android.view.View
↳ android.view.ViewGroup
↳ android.widget.FrameLayout
↳ android.widget.ViewAnimator
↳ android.widget.ViewSwitcher
↳ android.widget.ImageSwitcher

关于java - Unresolved reference : LayoutParams on Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53968513/

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