我用图标实现PagerTabStrip它会正常工作。但是当我更改标签时,我需要当前标签图标更改所选图像。
在我的代码下面
我的可绘制图标选择器代码
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_selected" android:state_selected="true" />
<item android:drawable="@drawable/ic_home_normal" android:state_selected="false" />
<item android:drawable="@drawable/ic_home_normal" />
</selector>
MainActivity.java
viewPager = (ViewPager) findViewById(R.id.pager);
tabs = (PagerTabStrip) findViewById(R.id.tabs);
fragments = new ArrayList<>();
fragments.add(R.drawable.menu_home_bg);
fragments.add(R.drawable.menu_likes_bg);
fragments.add(R.drawable.menu_matches_bg);
fragments.add(R.drawable.menu_home_bg);
pageAdapter = new TabPagerAdapter(getApplicationContext(), getSupportFragmentManager(), fragments);
viewPager.setAdapter(pageAdapter);
在我的适配器类中添加图标
Drawable image;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
image = context.getResources().getDrawable(tabIcon.get(position), context.getTheme());
} else
image = context.getResources().getDrawable(tabIcon.get(position));
if (image != null)
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableStringBuilder sb = new SpannableStringBuilder(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
帮助我如何在更改标签时更改选择器图标
答案 0 :(得分:1)
您无法在网页视图中引用此路径。您可能需要将css文件存储在assets文件夹中并动态引用它:
将CSS放在assets文件夹中,使用HTML进行操作,但是通过相对路径引用CSS,并通过loadDataWithBaseURL()方法将HTML加载到WebView:
webView.loadDataWithBaseURL("file:///android_asset/", htmlString, "text/html", "utf-8", null);
E.g. you have styles.css file, put it to assets folder, create HTML and load it:
StringBuilder sb = new StringBuilder();
sb.append("<HTML><HEAD><LINK href=\"styles.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
sb.append(tables.toString());
sb.append("</body></HTML>");
webView.loadDataWithBaseURL("file:///android_asset/", sb.toString(), "text/html","utf-8", null);