Android widget中的一个TextView中的字体设置不同大小

在Android桌面widget中实现一个textview显示两种大小的字体,效果图如下

关键代码如下:

               // 歌曲名称
                String songTitle = curSongInfo.getTitle();
                // 没有歌曲名,则显示“未知”
                if (TextUtils.isEmpty(songTitle) || songTitle.equals("null"))
                {
                    songTitle = context.getResources().getString(R.string.unKnow);
                }
                // 艺术家名称
                String artistname = curSongInfo.getArtistname();
                if (TextUtils.isEmpty(artistname) || artistname.equals("artistname"))
                {
                    artistname = context.getResources().getString(R.string.unKnow);
                }
                String showNanme = songTitle+" — " + artistname;
                Spannable WordtoSpan = new SpannableString(showNanme);
                // 设置歌曲名的文字大小
                WordtoSpan.setSpan(new AbsoluteSizeSpan(context.getResources().getDimensionPixelSize(R.dimen.text_size_30sp)), 0, songTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                // 设置艺术名的文字大小
                WordtoSpan.setSpan(new AbsoluteSizeSpan(context.getResources().getDimensionPixelSize(R.dimen.text_size_22sp)), songTitle.length(), showNanme.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                remoteView.setCharSequence(R.id.tv_song, "setText", WordtoSpan);
                ComponentName componentName = new ComponentName(context, MusicPlayerWidget.class);
                AppWidgetManager.getInstance(context).updateAppWidget(componentName, remoteView);

参考:http://txlong-onz.iteye.com/blog/1142781

posted on 2016-08-16 14:39  白衣雨果  阅读(469)  评论(0编辑  收藏  举报