Android P visibilityawareimagebutton.setVisibility只能从同一个库组中调用

Kyl*_*ner 125 android kotlin android-jetpack android-9.0-pie

我试图用新的Android P FloatingActionButton这部分com.google.android.material.floatingactionbutton.FloatingActionButton和我得到这样的警告:

VisibilityAwareImageButton.setVisibility只能从同一个库组中调用(groupId = com.google.android.material)

import com.google.android.material.floatingactionbutton.FloatingActionButton
import android.view.View

class MainActivity : AppCompatActivity() {

    lateinit var demoFab: FloatingActionButton

    override fun onCreate(savedInstanceState: Bundle?) {
        demoFab = findViewById(R.id.demoFab)
        demoFab.visibility = View.VISIBLE  // the warning is here
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我尝试过搜索,唯一的搜索结果是关于响应UI可见性更改:

https://developer.android.com/training/system-ui/visibility

我试着探索如何VISIBLE在该com.google.android.material包中找到一个int值,并且我找到的唯一一个是com.google.android.material.floatingactionbutton.FloatingActionButton.VISIBLE,但警告仍然存在.

顶级build.gradle

buildscript {
    ext.kotlin_version = '1.2.41'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha14'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.gms:oss-licenses:0.9.2"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "http://oss.sonatype.org/content/repositories/snapshots/" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

项目级build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.google.gms.oss.licenses.plugin'

android {
    compileSdkVersion 'android-P'
    defaultConfig {
        applicationId "com.codeforsanjose.maps.pacmap"
        minSdkVersion 21
        targetSdkVersion 'P'
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    splits {
        abi {
            enable true
            reset()
            include 'arm64-v8a', 'armeabi', 'armeabi-v7a', 'mips', 'x86', 'x86_64'
            universalApk false
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.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.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'

    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:5.5.2'
    //implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.1.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-plugin-locationlayer:0.5.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation:0.13.0'
    implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.13.0'

    implementation 'com.google.android.gms:play-services-oss-licenses:15.0.1'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.squareup.moshi:moshi:1.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.4.0'
    implementation "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
    implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
}
Run Code Online (Sandbox Code Playgroud)

编辑:

我应该注意到我正在使用Android Studio 3.2版金丝雀14.这个版本似乎有一些报道的错误,我怀疑这是其中之一.

编辑2:

Android Studio版本3.2 canary 15仍然存在问题,但我找到了使用show()和的解决方法hide()

override fun onCreate(savedInstanceState: Bundle?) {
    demoFab = findViewById(R.id.demoFab)
    demoFab.show()    // this works and doesn't have the warning
}
Run Code Online (Sandbox Code Playgroud)

Akh*_*ari 234

使用方法1

demoFab.show(); // in place of visible
demoFab.hide(); // in place of Invisible suppress the warning/error for me.
Run Code Online (Sandbox Code Playgroud)

和方法2

@SuppressLint("RestrictedApi") // also suppressed the warning
private void setUp() {
    ....
}
Run Code Online (Sandbox Code Playgroud)

  • 这也增加了漂亮的动画! (2认同)

And*_*eas 8

似乎可以正常使用,只是将其投射到视图上即可。

(mFloatingActionButton as View).visibility = INVISIBLE

当然,您需要记住可见性可能会影响其他组件,因此您可能应该同时使用show()hide()确保将更改通知其他组件。

  • 如果我想将可见性设置为“消失”而不是“不可见”,该怎么办? (5认同)

Ril*_*nda 6

使用:

 myButton.hide();
 myClearButton.hide();
Run Code Online (Sandbox Code Playgroud)

一个典型的例子是:

当用户正在键入或专注于EditText资源时隐藏和显示按钮:

检查用户是否正在键入或具有焦点:

 mCommentField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if (hasFocus) {
                //user has focused
                showBts();

            } else {
                //focus has stopped perform your desired action
                hideButtons();
            }
        }


    });
Run Code Online (Sandbox Code Playgroud)

隐藏和显示按钮方法:

private void hideButtons() {

    mCommentButton.hide();
    mClearButton.hide();
}

private void showBts() {

    mCommentButton. show();
    mClearButton.show();
Run Code Online (Sandbox Code Playgroud)

并且在您的xml中,默认情况下将按钮设置为不可见,以便仅在用户具有焦点或正在键入时显示/显示按钮:

android:visibility="invisible"
Run Code Online (Sandbox Code Playgroud)

最佳实践:

 android:visibility="Gone"
Run Code Online (Sandbox Code Playgroud)

使用可见性消失意味着您的视图不占用布局中的任何空间,而“不可见”将占用布局中的不必要的空间

在此示例中:“我的视图”位于ViewHolder中,并且iam使用recylerview引用片段中的按钮