android - setBackground with checking android version not working

家住魔仙堡 提交于 2020-02-23 06:44:33

问题


I want change the background drawable of a button. My min. SDK version is 14. Therefore I check the current version, which is installed on the device:

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    buttonClubPublic.setBackgroundDrawable(buttonClubPublic.getContext().getResources().getDrawable(R.drawable.check_button));
    buttonClubPrivate.setBackgroundDrawable(buttonClubPrivate.getContext().getResources().getDrawable(R.drawable.uncheck_button));
} else {
    buttonClubPublic.setBackground(buttonClubPublic.getContext().getResources().getDrawable(R.drawable.check_button));
    buttonClubPrivate.setBackground(buttonClubPrivate.getContext().getResources().getDrawable(R.drawable.uncheck_button));
}

When the current version is equal or above 16 I use setBackground otherwise I use setBackgroundDrawable.

However, it seems that Eclipse doesn't recognize the if/else check of the version, because I get an error:

Error: Call requires API level 16 (current min is 14)

I set the button in XML:

<Button
    android:id="@+id/create_club3_club_privat"
    android:text="@string/create_club3_privat"
    android:textColor="#000000"
    android:textSize="@dimen/check_button_size"
    android:layout_width="@dimen/check_button_width"
    android:layout_height="wrap_content"
    android:background="@drawable/uncheck_button"
    android:layout_marginLeft="@dimen/create_team_check_button_margin"
/>

来源:https://stackoverflow.com/questions/26549550/android-setbackground-with-checking-android-version-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!