webview中设置字体大小以及列表字体大小

本文介绍了如何在WebView中设置字体大小以及列表字体大小的方法,通过使用SharedPreferences来存储和获取字体大小设置,实现动态调整字体的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先在按钮那边需要做存储
SharedPreUtil.saveInt(DetailContentActivity.this, Constant.TEXT_SIZE,channelType);
intTosize(channelType);
public class SharedPreUtil {
    private static final String SMART_BJ = "smart_bj"; //存储是否第一次启动
    //存储boolean值
    public static void saveBoolean(Context context, String keyName , boolean value){
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putBoolean(keyName,value).apply();
    }
    //获取boolean值
    public static boolean getBoolean(Context context, String keyName){
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getBoolean(keyName,false);
    }

    public static void saveInt(Context context, String keyName , int value){
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putInt(keyName,value).commit();
    }
    public static int getInt(Context context, String keyName){
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt(keyName,2);
    }



    private static SharedPreferences getSharedPreferences(Context context){
        return context.getSharedPreferences(SMART_BJ, Context.MODE_PRIVATE);
    }
}
private void intTosize(int which) {
        switch (which) {
            case MainBottomLayout.ChannelType.CHANNEL_SMALL:
                textSize_sp.edit().putInt(Constant.TEXTSIZE_INTEGER,
                        Constant.TEXTSIZE_SMALL).commit();//这一行代表列表字体初始化
                settings.setTextSize(WebSettings.TextSize.SMALLER);//这一行代表webview中字体大小
                break;
            case MainBottomLayout.ChannelType.CHANNEL_MIDDLE:
                textSize_sp.edit().putInt(Constant.TEXTSIZE_INTEGER,
                        Constant.TEXTSIZE_CENTRE).commit();
                settings.setTextSize(WebSettings.TextSize.NORMAL);
                break;
            case MainBottomLayout.ChannelType.CHANNEL_BIG:
                textSize_sp.edit().putInt(Constant.TEXTSIZE_INTEGER,
                        Constant.TEXTSIZE_BIG).commit();
                settings.setTextSize(WebSettings.TextSize.LARGER);
                break;
            case MainBottomLayout.ChannelType.CHANNEL_BIGGER:
                textSize_sp.edit().putInt(Constant.TEXTSIZE_INTEGER,
                        Constant.TEXTSIZE_MAX_BIG).commit();
                settings.setTextSize(WebSettings.TextSize.LARGEST);
                break;
        }
        
    }

ok,初始化这边就开始了,接着是webview中要怎么做处理呢

 

int textSize = SharedPreUtil.getInt(this, Constant.TEXT_SIZE);//获取到webview字体
intTosize(textSize);

以上就是webview中字体大小了

接下来是最后一个,那就是列表的字体大小了,这个相信大家都不难吧

holder.tv_shorttitle.setTextSize(context.getSharedPreferences(Constant.TEXT_SIZE1, context.MODE_PRIVATE).getInt(Constant
        .TEXTSIZE_INTEGER, Constant.TEXTSIZE_CENTRE));

就是这句啦,在原本上面的那句先初始化了列表字体大小,在这里直接获取。

以上就是字体大小的设置了,结合在一起用了好一会儿,哈哈

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值