草庐IT

安卓工作室 : Error: Expected resource of type styleable [ResourceType]

coder 2023-12-18 原文

在android studio中,当我想要生成签名的apk时,会导致以下错误:

错误:错误:类型为可样式 [ResourceType] 的预期资源

这个类是:https://github.com/astuetz/PagerSlidingTabStrip

如何修复错误?

PagerSlidingTabStrip 类:

import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Paint.Style;
    import android.graphics.Typeface;
    import android.os.Build;
    import android.os.Parcel;
    import android.os.Parcelable;
    import android.support.v4.view.ViewPager;
    import android.support.v4.view.ViewPager.OnPageChangeListener;
    import android.util.AttributeSet;
    import android.util.DisplayMetrics;
    import android.util.TypedValue;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewTreeObserver.OnGlobalLayoutListener;
    import android.widget.HorizontalScrollView;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;
    import android.widget.TextView;

    import java.util.Locale;

    public class PagerSlidingTabStrip extends HorizontalScrollView {

        public interface IconTabProvider {
            public int getPageIconResId(int position);
        }

        // @formatter:off
        private static final int[] ATTRS = new int[] {
            android.R.attr.textSize,
            android.R.attr.textColor
        };
        // @formatter:on

        private LinearLayout.LayoutParams defaultTabLayoutParams;
        private LinearLayout.LayoutParams expandedTabLayoutParams;

        private final PageListener pageListener = new PageListener();
        public OnPageChangeListener delegatePageListener;

        private LinearLayout tabsContainer;
        private ViewPager pager;

        private int tabCount;

        private int currentPosition = 0;
        private float currentPositionOffset = 0f;

        private Paint rectPaint;
        private Paint dividerPaint;

        private int indicatorColor = 0xFF666666;
        private int underlineColor = 0x1A000000;
        private int dividerColor = 0x1A000000;

        private boolean shouldExpand = false;
        private boolean textAllCaps = true;

        private int scrollOffset = 52;
        private int indicatorHeight = 8;
        private int underlineHeight = 2;
        private int dividerPadding = 12;
        private int tabPadding = 24;
        private int dividerWidth = 1;

        private int tabTextSize = 12;
        private int tabTextColor = 0xFF666666;
        private Typeface tabTypeface = null;
        private int tabTypefaceStyle = Typeface.BOLD;

        private int lastScrollX = 0;

        private int tabBackgroundResId = R.drawable.background_tab;

        private Locale locale;

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

        public PagerSlidingTabStrip(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

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

            setFillViewport(true);
            setWillNotDraw(false);

            tabsContainer = new LinearLayout(context);
            tabsContainer.setOrientation(LinearLayout.HORIZONTAL);
            tabsContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
            addView(tabsContainer);

            DisplayMetrics dm = getResources().getDisplayMetrics();

            scrollOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm);
            indicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm);
            underlineHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm);
            dividerPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm);
            tabPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm);
            dividerWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm);
            tabTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, tabTextSize, dm);

            // get system attrs (android:textSize and android:textColor)

            TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);

            tabTextSize = a.getDimensionPixelSize(0, tabTextSize);
            tabTextColor = a.getColor(1, tabTextColor);

            a.recycle();

            // get custom attrs

            a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);

            indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor);
            underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsUnderlineColor, underlineColor);
            dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsDividerColor, dividerColor);
            indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsIndicatorHeight, indicatorHeight);
            underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsUnderlineHeight, underlineHeight);
            dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsDividerPadding, dividerPadding);
            tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsTabPaddingLeftRight, tabPadding);
            tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_pstsTabBackground, tabBackgroundResId);
            shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsShouldExpand, shouldExpand);
            scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_pstsScrollOffset, scrollOffset);
            textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_pstsTextAllCaps, textAllCaps);

            a.recycle();

            rectPaint = new Paint();
            rectPaint.setAntiAlias(true);
            rectPaint.setStyle(Style.FILL);

            dividerPaint = new Paint();
            dividerPaint.setAntiAlias(true);
            dividerPaint.setStrokeWidth(dividerWidth);

            defaultTabLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
            expandedTabLayoutParams = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);

            if (locale == null) {
                locale = getResources().getConfiguration().locale;
            }
        }

        public void setViewPager(ViewPager pager) {
            this.pager = pager;

            if (pager.getAdapter() == null) {
                throw new IllegalStateException("ViewPager does not have adapter instance.");
            }

            pager.setOnPageChangeListener(pageListener);

            notifyDataSetChanged();
        }

        public void setOnPageChangeListener(OnPageChangeListener listener) {
            this.delegatePageListener = listener;
        }

        public void notifyDataSetChanged() {

            tabsContainer.removeAllViews();

            tabCount = pager.getAdapter().getCount();

            for (int i = 0; i < tabCount; i++) {

                if (pager.getAdapter() instanceof IconTabProvider) {
                    addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i));
                } else {
                    addTextTab(i, pager.getAdapter().getPageTitle(i).toString());
                }

            }

            updateTabStyles();

            getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

                @SuppressWarnings("deprecation")
                @SuppressLint("NewApi")
                @Override
                public void onGlobalLayout() {

                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }

                    currentPosition = pager.getCurrentItem();
                    scrollToChild(currentPosition, 0);
                }
            });

        }

        private void addTextTab(final int position, String title) {

            TextView tab = new TextView(getContext());
            tab.setText(title);
            tab.setGravity(Gravity.CENTER);
            tab.setSingleLine();

            addTab(position, tab);
        }

        private void addIconTab(final int position, int resId) {

            ImageButton tab = new ImageButton(getContext());
            tab.setImageResource(resId);

            addTab(position, tab);

        }

        private void addTab(final int position, View tab) {
            tab.setFocusable(true);
            tab.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem(position);
                }
            });

            tab.setPadding(tabPadding, 0, tabPadding, 0);
            tabsContainer.addView(tab, position, shouldExpand ? expandedTabLayoutParams : defaultTabLayoutParams);
        }

        private void updateTabStyles() {

            for (int i = 0; i < tabCount; i++) {

                View v = tabsContainer.getChildAt(i);

                v.setBackgroundResource(tabBackgroundResId);

                if (v instanceof TextView) {

                    TextView tab = (TextView) v;
                    tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
                    tab.setTypeface(tabTypeface, tabTypefaceStyle);
                    tab.setTextColor(tabTextColor);

                    // setAllCaps() is only available from API 14, so the upper case is made manually if we are on a
                    // pre-ICS-build
                    if (textAllCaps) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                            tab.setAllCaps(true);
                        } else {
                            tab.setText(tab.getText().toString().toUpperCase(locale));
                        }
                    }
                }
            }

        }

        private void scrollToChild(int position, int offset) {

            if (tabCount == 0) {
                return;
            }

            int newScrollX = tabsContainer.getChildAt(position).getLeft() + offset;

            if (position > 0 || offset > 0) {
                newScrollX -= scrollOffset;
            }

            if (newScrollX != lastScrollX) {
                lastScrollX = newScrollX;
                scrollTo(newScrollX, 0);
            }

        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (isInEditMode() || tabCount == 0) {
                return;
            }

            final int height = getHeight();

            // draw indicator line

            rectPaint.setColor(indicatorColor);

            // default: line below current tab
            View currentTab = tabsContainer.getChildAt(currentPosition);
            float lineLeft = currentTab.getLeft();
            float lineRight = currentTab.getRight();

            // if there is an offset, start interpolating left and right coordinates between current and next tab
            if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

                View nextTab = tabsContainer.getChildAt(currentPosition + 1);
                final float nextTabLeft = nextTab.getLeft();
                final float nextTabRight = nextTab.getRight();

                lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
                lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
            }

            canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

            // draw underline

            rectPaint.setColor(underlineColor);
            canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

            // draw divider

            dividerPaint.setColor(dividerColor);
            for (int i = 0; i < tabCount - 1; i++) {
                View tab = tabsContainer.getChildAt(i);
                canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
            }
        }

        private class PageListener implements OnPageChangeListener {

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

                currentPosition = position;
                currentPositionOffset = positionOffset;

                scrollToChild(position, (int) (positionOffset * tabsContainer.getChildAt(position).getWidth()));

                invalidate();

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

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    scrollToChild(pager.getCurrentItem(), 0);
                }

                if (delegatePageListener != null) {
                    delegatePageListener.onPageScrollStateChanged(state);
                }
            }

            @Override
            public void onPageSelected(int position) {
                if (delegatePageListener != null) {
                    delegatePageListener.onPageSelected(position);
                }
            }

        }

        public void setIndicatorColor(int indicatorColor) {
            this.indicatorColor = indicatorColor;
            invalidate();
        }

        public void setIndicatorColorResource(int resId) {
            this.indicatorColor = getResources().getColor(resId);
            invalidate();
        }

        public int getIndicatorColor() {
            return this.indicatorColor;
        }

        public void setIndicatorHeight(int indicatorLineHeightPx) {
            this.indicatorHeight = indicatorLineHeightPx;
            invalidate();
        }

        public int getIndicatorHeight() {
            return indicatorHeight;
        }

        public void setUnderlineColor(int underlineColor) {
            this.underlineColor = underlineColor;
            invalidate();
        }

        public void setUnderlineColorResource(int resId) {
            this.underlineColor = getResources().getColor(resId);
            invalidate();
        }

        public int getUnderlineColor() {
            return underlineColor;
        }

        public void setDividerColor(int dividerColor) {
            this.dividerColor = dividerColor;
            invalidate();
        }

        public void setDividerColorResource(int resId) {
            this.dividerColor = getResources().getColor(resId);
            invalidate();
        }

        public int getDividerColor() {
            return dividerColor;
        }

        public void setUnderlineHeight(int underlineHeightPx) {
            this.underlineHeight = underlineHeightPx;
            invalidate();
        }

        public int getUnderlineHeight() {
            return underlineHeight;
        }

        public void setDividerPadding(int dividerPaddingPx) {
            this.dividerPadding = dividerPaddingPx;
            invalidate();
        }

        public int getDividerPadding() {
            return dividerPadding;
        }

        public void setScrollOffset(int scrollOffsetPx) {
            this.scrollOffset = scrollOffsetPx;
            invalidate();
        }

        public int getScrollOffset() {
            return scrollOffset;
        }

        public void setShouldExpand(boolean shouldExpand) {
            this.shouldExpand = shouldExpand;
            requestLayout();
        }

        public boolean getShouldExpand() {
            return shouldExpand;
        }

        public boolean isTextAllCaps() {
            return textAllCaps;
        }

        public void setAllCaps(boolean textAllCaps) {
            this.textAllCaps = textAllCaps;
        }

        public void setTextSize(int textSizePx) {
            this.tabTextSize = textSizePx;
            updateTabStyles();
        }

        public int getTextSize() {
            return tabTextSize;
        }

        public void setTextColor(int textColor) {
            this.tabTextColor = textColor;
            updateTabStyles();
        }

        public void setTextColorResource(int resId) {
            this.tabTextColor = getResources().getColor(resId);
            updateTabStyles();
        }

        public int getTextColor() {
            return tabTextColor;
        }

        public void setTypeface(Typeface typeface, int style) {
            this.tabTypeface = typeface;
            this.tabTypefaceStyle = style;
            updateTabStyles();
        }

        public void setTabBackground(int resId) {
            this.tabBackgroundResId = resId;
        }

        public int getTabBackground() {
            return tabBackgroundResId;
        }

        public void setTabPaddingLeftRight(int paddingPx) {
            this.tabPadding = paddingPx;
            updateTabStyles();
        }

        public int getTabPaddingLeftRight() {
            return tabPadding;
        }

        @Override
        public void onRestoreInstanceState(Parcelable state) {
            SavedState savedState = (SavedState) state;
            super.onRestoreInstanceState(savedState.getSuperState());
            currentPosition = savedState.currentPosition;
            requestLayout();
        }

        @Override
        public Parcelable onSaveInstanceState() {
            Parcelable superState = super.onSaveInstanceState();
            SavedState savedState = new SavedState(superState);
            savedState.currentPosition = currentPosition;
            return savedState;
        }

        static class SavedState extends BaseSavedState {
            int currentPosition;

            public SavedState(Parcelable superState) {
                super(superState);
            }

            private SavedState(Parcel in) {
                super(in);
                currentPosition = in.readInt();
            }

            @Override
            public void writeToParcel(Parcel dest, int flags) {
                super.writeToParcel(dest, flags);
                dest.writeInt(currentPosition);
            }

            public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
                @Override
                public SavedState createFromParcel(Parcel in) {
                    return new SavedState(in);
                }

                @Override
                public SavedState[] newArray(int size) {
                    return new SavedState[size];
                }
            };
        }

    }

值/attrs.xml

<declare-styleable name="PagerSlidingTabStrip">
        <attr name="pstsIndicatorColor" format="color" />
        <attr name="pstsUnderlineColor" format="color" />
        <attr name="pstsDividerColor" format="color" />
        <attr name="pstsIndicatorHeight" format="dimension" />
        <attr name="pstsUnderlineHeight" format="dimension" />
        <attr name="pstsDividerPadding" format="dimension" />
        <attr name="pstsTabPaddingLeftRight" format="dimension" />
        <attr name="pstsScrollOffset" format="dimension" />
        <attr name="pstsTabBackground" format="reference" />
        <attr name="pstsShouldExpand" format="boolean" />
        <attr name="pstsTextAllCaps" format="boolean" />
    </declare-styleable>

最佳答案

我也遇到了同样的问题所以我解决了

设置

@SuppressWarnings("ResourceType")

在你的类里面 PagerSlidingTabStrip

@SuppressWarnings("ResourceType") public class PagerSlidingTabStrip extends HorizontalScrollView { ..... }

并在默认配置下的 gradle 中:

lintOptions { disable "ResourceType"

    }

关于安卓工作室 : Error: Expected resource of type styleable [ResourceType],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37078822/

有关安卓工作室 : Error: Expected resource of type styleable [ResourceType]的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby-on-rails - Ruby on Rails : . 常量化 : wrong constant name error? - 2

    我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby​​常量:Content2而不是content2。Aconstantnamestart

  4. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  5. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  6. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  7. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  8. ruby - RVM "ERROR: Unable to checkout branch ."单用户 - 2

    我在新的Debian6VirtualBoxVM上安装RVM时遇到问题。我已经安装了所有需要的包并使用下载了安装脚本(curl-shttps://rvm.beginrescueend.com/install/rvm)>rvm,但以单个用户身份运行时bashrvm我收到以下错误消息:ERROR:Unabletocheckoutbranch.安装在这里停止,并且(据我所知)没有安装RVM的任何文件。如果我以root身份运行脚本(对于多用户安装),我会收到另一条消息:Successfullycheckedoutbranch''安装程序继续并指示成功,但未添加.rvm目录,甚至在修改我的.bas

  9. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  10. ruby - `rescue $!` 是如何工作的? - 2

    我知道全局变量$!包含最新的异常对象,但我对下面的语法感到困惑。谁能帮助我理解以下语法?rescue$! 最佳答案 此构造可防止异常停止您的程序并使堆栈跟踪冒泡。它还会将该异常作为值返回,这很有用。a=get_me_datarescue$!在此行之后,a将保存请求的数据或异常。然后您可以分析该异常并采取相应措施。defget_me_dataraise'Nodataforyou'enda=get_me_datarescue$!puts"Executioncarrieson"pa#>>Executioncarrieson#>>#更现实的

随机推荐