【问题标题】:Tab icons disappeared in marshmallow标签图标在棉花糖中消失了
【发布时间】:2018-01-23 12:46:06
【问题描述】:

我已经定义了三个不同的类来处理标签图标。下面是BaseActivity类。

public class MainScreenPagerAdapter extends FragmentPagerAdapter implements TabBarView.IconTabProvider {

private int[] tab_icons =
        {R.drawable.launcher_inactive,
                R.drawable.chat_inactive,
                R.drawable.t3_inactive,
                R.drawable.launcher,
                R.drawable.chat_active,
                R.drawable.t3_active
         };

然后在TabView.java我定义了setIcon如下

public void setIcon(int resId) {
    setIcon(getContext().getResources().getDrawable(resId));
 }

由于getDrawable 已弃用,我认为这是我看不到标签图标的唯一问题

public class TabView extends LinearLayout {

private ImageView mImageView;
private TextView mTextView;

public TabView(Context context) {
    this(context, null);
}

public TabView(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.actionBarTabStyle);
}

public TabView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    TypedValue outValue = new TypedValue();
    context.getTheme().resolveAttribute(android.R.attr.actionBarTabTextStyle, outValue, true);

    int txtstyle = outValue.data;

    int pad = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
            .getDisplayMetrics());

    mImageView = new ImageView(context);
    mImageView.setLayoutParams(new LayoutParams(100, LayoutParams.MATCH_PARENT));//hadi andaze icon taba
    mImageView.setScaleType(ScaleType.CENTER_INSIDE);

    mTextView = new TextView(context);
    mTextView.setLayoutParams(new LayoutParams(50, LayoutParams.MATCH_PARENT));
    mTextView.setGravity(Gravity.CENTER);
    // mTextView.setCompoundDrawablePadding(pad);
    mTextView.setTextAppearance(context, txtstyle);
    ;

    this.addView(mImageView);
    // this.addView(mTextView);

    this.setLayoutParams(new LayoutParams(determineScreenDensity(), LayoutParams.MATCH_PARENT));
    //this.setLayoutParams(new LayoutParams(150, LayoutParams.MATCH_PARENT));
}

public void setIcon(int resId) {
    Log.i("Case+","" +resId);
    setIcon(getContext().getResources().getDrawable(resId));

}

public void setIcon(Drawable icon) {
    if (icon != null) {
        mImageView.setVisibility(View.VISIBLE);
        mImageView.setImageDrawable(icon);
    } else {
        mImageView.setImageResource(View.GONE);
        Log.i("icon","null" +null);
    }
}

public void setText(int resId, int ico) {
    setText(getContext().getString(resId), ico);
}

public void setText(CharSequence text, int ico) {
    mTextView.setText(text);
    mTextView.setCompoundDrawablesWithIntrinsicBounds(ico, 0, 0, 0);
    ;
}

然后从TabBarView.java类我叫setIcon()

 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    mSelectedTab = position;
    mOffset = positionOffset;

    invalidate();

    if (delegatePageListener != null) {
        delegatePageListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    if (position == 0) {

        tabView1.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(3));
        tabView1.setScaleX(1);
        tabView1.setScaleY(1);


        tabView2.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(1));
      //  tabView2.setScaleX(2);
      //  tabView2.setScaleY(2);


        tabView3.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(2));
        tabView3.setScaleX(1.1f);
        tabView3.setScaleY(1.1f);

    } else if (position == 1) {
        tabView1.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(0));
        tabView1.setScaleX(0.7f);
        tabView1.setScaleY(0.7f);

       // tabView1.setBackgroundResource(R.drawable.transparent_button_2);

        tabView2.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(4));
        //tabView2.setBackgroundResource(R.drawable.transparent_round_button);
        //tabView2.setScaleX(2);
        //tabView2.setScaleY(2);


        tabView3.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(2));
        tabView3.setScaleX(1.1f);
        tabView3.setScaleY(1.1f);
       // tabView3.setBackgroundResource(R.drawable.transparent_button_2);

    } else if (position == 2) {
        tabView1.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(0));

        //tabView1.setBackgroundResource(R.drawable.transparent_button_2);
        tabView2.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(1));

        //tabView2.setBackgroundResource(R.drawable.transparent_button_2);
        tabView3.setIcon(((IconTabProvider) pager.getAdapter()).getPageIconResId(5));
      //  tabView3.setBackgroundResource(R.drawable.transparent_round_button);

       tabView3.setScaleX(1.7f);
       tabView3.setScaleY(1.7f);
    /*
       LayoutParams params = new LayoutParams(
                LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT
        );
        params.gravity = Gravity.CENTER;
        params.setMargins(40,0,0,0);
        tabView1.setLayoutParams(params);
        tabView2.setLayoutParams(params);
        tabView3.setLayoutParams(params);
     // tabView2.setPadding(20,0,20,0);

       //tabView3.setPadding(20,0,0,0);
        */

         }
      }

this question 中的解决方案无法应用于我的代码。如果您能帮我解决这个问题,我将不胜感激。

【问题讨论】:

  • 您从哪里以及如何调用setIcon(int) 方法?
  • @azizbekian 我在 TabView 类中定义了 setIcon(int)。上面提供了这个类
  • 问题是您从哪里调用setIcon() 而不是setIcon() 声明的位置。
  • @azizbekian 对不起。我从 TabBarView 调用了setIcon。 java类。我将代码添加到问题中。

标签: android android-drawable


【解决方案1】:

在我的情况下这项工作

         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    holder.thumbnail.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher_background, context.getTheme()));
                } else {
                    holder.thumbnail.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_launcher_background));
                }

//根据你的要求

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    setIcon(context.getResources().getDrawable(R.drawable.ic_launcher_background, context.getTheme()));
                } else {
                    setIcon(context.getResources().getDrawable(R.drawable.ic_launcher_background));
                }

【讨论】:

  • 您的解决方案无法应用于我的结构。请阅读我的解释
  • 哪些解释?
  • @kalabalik setIcon 在不同的类中定义,我从另一个类中调用它。 R.drawable.name 也在 MainScreenPager 中声明。
【解决方案2】:

您的TabView 代码看起来非常简单。我能想到的唯一替代方法是您可以尝试在设置可绘制对象后在setIcon() 中调用invalidate(),然后尝试重绘布局。

【讨论】:

    【解决方案3】:

    如果它在 API 级别 23 以下工作,那么我认为您在使用 getDrawable 时遇到问题,我们有一些选项可以用来代替 getContext().getResources().getDrawable()

    可以和ContextCompat一起使用

    ContextCompat.getDrawable(getActivity(), R.drawable.drawble_name);
    

    请尝试此操作,因为在 API 级别 22 以上不推荐使用 getDrawable 资源。

    【讨论】:

    • 由于我的代码结构,我无法使用它,这是确切的问题
    猜你喜欢
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多