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

时间:2018-05-15 06:30:12

标签: android kotlin android-9.0-pie android-jetpack

我正在尝试使用属于com.google.android.material.floatingactionbutton.FloatingActionButton的新Android P 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
    }
}

enter image description here

我尝试过搜索,唯一的搜索结果是关于响应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
}

项目级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'
}

编辑:

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

编辑2:

Android Studio 3.2版金丝雀15仍存在问题,但我找到了使用show()hide()

的解决方法
override fun onCreate(savedInstanceState: Bundle?) {
    demoFab = findViewById(R.id.demoFab)
    demoFab.show()    // this works and doesn't have the warning
}

8 个答案:

答案 0 :(得分:159)

使用方法1

RecycleView

和方法2

demoFab.show(); // in place of visible
demoFab.hide(); // in place of Invisible suppress the warning/error for me.

答案 1 :(得分:3)

似乎可以很好地将其投射到视图。

(mFloatingActionButton as View).visibility = INVISIBLE

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

答案 2 :(得分:2)

这也有效:

findViewById(R.id.fab).setVisibility(View.GONE);

答案 3 :(得分:2)

使用:

 myButton.hide();
 myClearButton.hide();

一个典型的例子是:

在用户键入或专注于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();
            }
        }


    });

隐藏和显示按钮方法:

private void hideButtons() {

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

private void showBts() {

    mCommentButton. show();
    mClearButton.show();

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

android:visibility="invisible"

最佳做法:

 android:visibility="Gone"

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

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

答案 4 :(得分:1)

对我来说,上述方法无效,因此我创建了扩展功能以使其起作用。

fun View.showView() {
    this.visibility = View.VISIBLE
}

fun View.hideView() {
    this.visibility = View.GONE
}

现在喜欢打

binding.fabAdded.showView()

答案 5 :(得分:0)

对于Kotlin,我有一个扩展方法

fun viewsVisibility(visibility: Int, vararg views: View) {
    for (view in views) { view.visibility = visibility }
}

然后在代码中,您可以执行以下操作

viewsVisibility(View.VISIBLE, demoFab) 
viewsVisibility(View.GONE, demoFab)
viewsVisibility(View.INVISIBLE, demoFab, addFab, removeFab)

该错误将消失,这为任何可见性状态提供了灵活性,同时还提供了要处理的视图列表。如最终示例行所示,很多时候我需要一次处理多个视图。

答案 6 :(得分:0)

if(data){
            fragmentPendingApprovalDetailsBinding.fabPendingList.show();
        }else {
            fragmentPendingApprovalDetailsBinding.fabPendingList.hide();
        }

答案 7 :(得分:0)

对于com.google.android.material.floatingactionbutton.FloatingActionButton

可见度

使用show和hide方法来动画化FloatingActionButton的可见性。显示动画会放大小部件并使其淡入,而隐藏动画会缩小小部件并使其淡出。

来源:https://material.io/develop/android/components/floating-action-button/

对于android.support.design.widget.FloatingActionButton 使用setVisibility()方法