我想在TabView上添加图标,但由于无法getResources()在自定义TabAdapter中使用而被卡住。我已经尝试使用getAplicationContext().getResources, getActivity.getResources(),但是仍然看到该消息"cannot resolve method"。有人可以帮我吗?
package com.glapps.mobile.codifynotes.Adapter;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import com.glapps.mobile.codifynotes.Activity.MainActivity;
import com.glapps.mobile.codifynotes.Fragment.CodifyFragment;
import com.glapps.mobile.codifynotes.Fragment.Configuracoes.ConfiguracoesFragment;
import com.glapps.mobile.codifynotes.Fragment.DecodifyFragment;
import com.glapps.mobile.codifynotes.R;
public class TabAdapter extends FragmentStatePagerAdapter {
private String[] tituloAba = {"CODIFICAR", "DECODIFICAR", "CONFIGURAÇÕES"};
private int[] imageResId = {
R.drawable.ic_action_lock_closed,
R.drawable.ic_action_lock_closed,
R.drawable.ic_action_settings_white
};
public TabAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position){
case 0: …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个新闻部分,它从我的网站加载一些新闻,其中一些包含图像,所以我从互联网加载它们。但是当图像没有加载时,有一个绿色方块只有在图像加载时才会消失。
图片未加载:
然后加载图像:
我想让那个绿色方块不可见。
为简单起见,假设我什至不加载图像,只是想让绿色方块不可见,而不用空文本替换图像标签。
代码:
val exampleText = "Example <br> <img src=\"https://www.w3schools.com/images/w3schools_green.jpg\" alt=\"W3Schools.com\"> <br> Example"
tv_body.text = fromHtml(exampleText)
fun fromHtml(html: String?): Spanned? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
} else {
Html.fromHtml(html);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不做任何恶作剧的情况下更改默认图像?
我的解决方法:
我为解决这个问题所做的是调整自定义 fromHtml 函数。
private var drawable: Drawable? = null
fun fromHtml(context: Activity?, tv: TextView?, text: String?) {
if (TextUtils.isEmpty(text) || context == null || tv == null) return
//Replace all image tags with an empty text
val noImageText = text!!.replace("<img.*?>".toRegex(), …Run Code Online (Sandbox Code Playgroud)