未解决的裁判:Kotlin上的LayoutParams

时间:2018-12-29 10:00:42

标签: java android kotlin

我无法将我们的功能从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;
    }
}

在科特林中像波纹管一样

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上运行良好。但是不幸的是,科特林没有意识到这些功能。

这是我的Java Apps 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上显示语法错误

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

2 个答案:

答案 0 :(得分:0)

我认为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 

答案 1 :(得分:0)

无论何时您想将Java转换为Kotlin,但都无法解析引用。只需看一下Java文件的import部分,然后您可以找到应该导入Kotlin文件的文件。

对于您的问题,LayoutParams是从FrameLayout.LayoutParams导入的。

enter image description here

enter image description here