You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Marshmallow系统下自定义Tab图标消失问题求助

Fixing Tab Icon Not Showing Issue in Your Android Code

Hey there! Let's work through this Tab icon display problem you're dealing with. You’re correct that the deprecated getDrawable() method is a key issue here, but there are a couple of other small mistakes we need to fix too. Let's go step by step:

1. Replace the Deprecated getDrawable() Method

Starting from API level 21, getResources().getDrawable(int) was deprecated. Using it can cause compatibility issues, especially on newer Android versions. Instead, use ContextCompat.getDrawable() which handles compatibility across all API levels seamlessly.

Update your setIcon(int resId) method in TabView.java:

public void setIcon(int resId) {
    Log.i("Case+","" + resId);
    // Use ContextCompat to get drawable safely
    Drawable icon = ContextCompat.getDrawable(getContext(), resId);
    setIcon(icon);
}

Note: Make sure you have the AndroidX AppCompat library added to your project (most modern Android projects do, but double-check if you run into errors).

2. Fix the Icon Visibility Bug in setIcon(Drawable)

Looking at your code, there's a critical mistake in the setIcon(Drawable) method: when the icon is null, you're calling mImageView.setImageResource(View.GONE) instead of setting the view's visibility. View.GONE is a constant for visibility, not a valid drawable resource ID—this would cause the ImageView to fail to load any valid resource, or even crash.

Fix that method like this:

public void setIcon(Drawable icon) {
    if (icon != null) {
        mImageView.setVisibility(View.VISIBLE);
        mImageView.setImageDrawable(icon);
    } else {
        mImageView.setVisibility(View.GONE); // Correctly set visibility to GONE
        Log.i("icon","null: " + icon);
    }
}

3. Check for Additional Potential Issues

Even after fixing the above, let's verify a few more things to rule out other problems:

  • Verify Drawable Resources: Ensure your R.drawable.launcher_inactive, R.drawable.chat_active, etc., exist in the correct res/drawable directories (and variants like drawable-hdpi if needed). Check Logcat for ResourceNotFoundException errors—this would indicate a missing resource.
  • Fix ImageView Layout Parameters: Your current code sets the ImageView width to 100 (raw pixels), which will look tiny on high-resolution screens. Convert it to dp for consistent sizing:
    int iconWidth = (int) TypedValue.applyDimension(
        TypedValue.COMPLEX_UNIT_DIP, 
        100, 
        getResources().getDisplayMetrics()
    );
    mImageView.setLayoutParams(new LayoutParams(iconWidth, LayoutParams.MATCH_PARENT));
    
  • Validate Icon Resource Indices: In your MainScreenPagerAdapter, the tab_icons array has inactive icons first (indices 0-2) then active ones (3-5). Double-check that calls like getPageIconResId(3) in TabBarView.java are pointing to the correct icon you intend to display.

Final Notes

After applying these changes, rebuild your project and test again. The combination of fixing the deprecated drawable call, correcting the visibility logic, and ensuring proper resource/parameter setup should resolve the Tab icon not showing issue.

内容的提问来源于stack exchange,提问作者Hadi Samadbin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:08:37