線上直播系統原始碼,個人主頁使用者標籤新增實現

zhibo系統開發發表於2021-10-12

線上直播系統原始碼,個人主頁使用者標籤新增實現的相關程式碼

程式碼實現: 自定義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="
    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);


以上就是線上直播系統原始碼,個人主頁使用者標籤新增實現得相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2795518/,如需轉載,請註明出處,否則將追究法律責任。

相關文章