在线直播系统源码,个人主页用户标签添加实现

发布时间:2022-07-03 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在线直播系统源码,个人主页用户标签添加实现脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

在线直播系统码,个人主页用户标签添加实现的相关代码

代码实现: 自定义TextView

 

public class TagTextView extends AppCompatTextView {    PRivate View view;//标签布局的最外层布局    private Context mContext;    private TextView tv_tag;    private StringBuffer content_buffer;    //必须重写所有的构造器,否则可能会出现无法inflate布局的错误!    public TagTextView(Context context) {        suPEr(context);        mContext = context;    }    public TagTextView(Context context, AttributeSet attrs) {        super(context, attrs);        mContext = context;    }    public TagTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        mContext = context;    }    public void setContentAndTag(String content, List<String> tags) {        content_buffer = new StringBuffer();        for (String ITem : tags) {//将每个tag的内容添加到content后边,之后将用drawable替代这些tag所占的位置            content_buffer.append(item);        }        content_buffer.append(content);        SpannableString spannableString = new SpannableString(content_buffer);        for (int i = 0; i < tags.size(); i++) {            String item = tags.get(i);            View view = LayoutInflater.From(mContext).inflate(R.layout.layout_texttab, null);//R.layout.tag是每个标签的布局            tv_tag = view.findViewById(R.id.tv_tag);            tv_tag.setText(item);            Bitmap bitmap = convertViewToBitmap(view);            Drawable d = new BitmapDrawable(bitmap);            d.setBounds(0, 0, tv_tag.getWidth(), tv_tag.getHeight());//缺少这句的话,不会报错,但是图片不回显示            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);//图片将对齐底部边线            int startIndex;            int endIndex;            startIndex = getLastLength(tags, i );            endIndex = startIndex + item.length();            LOG.e("tag", "the start is" + startIndex + "the end is" + endIndex);            spannableString.setSpan(span, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);        }        setText(spannableString);        setGravity(Gravity.center_VERTical);    }    private static Bitmap convertViewToBitmap(View view) {        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());        view.buildDrawingCache();        Bitmap bitmap = view.getDrawingCache();        return bitmap;    }    private int getLastLength(List<String> list, int maxLength) {        int length = 0;        for (int i = 0; i < maxLength; i++) {            length += list.get(i).length();        }        return length;    }}

 

自定义一个layout_texttab布局

 

<?XMl version="1.0" encoding="utf-8"?><LinearLayout xMLns:andROId="http://schemas.android wangt.cc /apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:id="@+id/tv_tag"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_vertical"        android:layout_marginBottom="2dp"        android:layout_marginRight="5dp"        android:background="@drawable/round_blue"        android:gravity="center_vertical"        android:paddingBottom="2dp"        android:paddingLeft="3dp"        android:paddingRight="3dp"        android:text="标签"        android:textColor="@android:color/white"        android:textSize="12sp"/></LinearLayout>

 

 

使用

 

<com.zc.tool.TagTextView        android:id="@+id/text_tab"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="12dp"        android:textColor="@color/white"        android:gravity="center" /> List<String> tags = new ArrayList<>();        tags.add("@用户");        tags.add("@程序猿");        tags.add("@产品经理");        textTab.setContentAndTag("改个需求", tags);

 

以上就是在线直播系统源码,个人主页用户标签添加实现得相关代码, 更多内容欢迎关注之后的文章

 

脚本宝典总结

以上是脚本宝典为你收集整理的在线直播系统源码,个人主页用户标签添加实现全部内容,希望文章能够帮你解决在线直播系统源码,个人主页用户标签添加实现所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。