直播系统平台搭建,主播个性标签显示在id后面

直播系统平台搭建,主播个性标签显示在id后面实现的相关代码

具体实现

1.1文字限制一行时,标签在文字后面

文字较少时就像第一行这样,文字较多显示不下时就像二三行那样省略。

实现方法一,使用线性布局实现

 

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="不宽度,不确定字数"
                android:singleLine="true"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="跟随标签"
                android:background="@color/yellow_FF9B52"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="跟随标签2"
                android:background="@color/blue_74D3FF"/>
        </LinearLayout>

​实现方法二,使用约束布局实现

 

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/refund_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="长数据长数据长数据长数据长数据长数据长数据长数据长数据"
                app:layout_constrainedWidth="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0"
                app:layout_constraintHorizontal_chainStyle="packed"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toLeftOf="@id/refund_mark_num"
                app:layout_constraintTop_toTopOf="parent" />
            <TextView
                android:id="@+id/refund_mark_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/yellow_FF9B52"
                android:text="跟随标签"
                android:gravity="center"
                app:layout_constrainedWidth="true"
                app:layout_constraintLeft_toRightOf="@+id/refund_name"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

1.2标签在文字前面时

实现思路和上面标签在文字后面一样。第一行的标签是一个控件,标签后面的文字是一个单独的 TextView,第二行也是一个单独的 TextView。如果想限制文字行数,直接对第二行的 TextView 限制就行。

xml 布局:

 

 <!--        前面跟随标签-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="前面跟随标签"
            android:textColor="@color/white"
            android:background="@color/red"
            />
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="@dimen/m15">
            <TextView
                android:id="@+id/tv_rl_test_tagFront"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="标签"
                android:background="@color/yellow_FF9B52"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"/>
            <com.kiwilss.xview.widget.textview.AlignTextView
                android:id="@+id/tv_rl_test_frontOne"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="任意显示一行任意显示一行任意显示一行任意显示一行任意显示一行"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toEndOf="@+id/tv_rl_test_tagFront"
                android:maxLines="1"
                />
            <com.kiwilss.xview.widget.textview.AlignTextView
                android:id="@+id/tv_rl_test_frontTwo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="任意显示一行任意显示一行任意显示一行任意显示一行任意显示一行"
                app:layout_constraintTop_toBottomOf="@+id/tv_rl_test_tagFront"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

activity 部分:

 

  //前面加标签
        TextView tvFront = (TextView) findViewById(R.id.tv_rl_test_tagFront);
        TextView tvFrontOne =  findViewById(R.id.tv_rl_test_frontOne);
        TextView tvFrontTwo =  findViewById(R.id.tv_rl_test_frontTwo);
        tvFrontOne.setText(tagSrc);
        //获取tvFrontOne显示的内容
        tvFrontOne.post(new Runnable() {
            @Override
            public void run() {
                //获取第一行显示的内容
                String lineContent = Utils.INSTANCE.getTextLineContent(tvFrontOne, 0, tagSrc);
                if (TextUtils.equals(lineContent,tagSrc)){
                    //一行可以完整显示
                    tvFrontTwo.setVisibility(View.GONE);
                }else {
                    //需要多行才能显示
                    tvFrontTwo.setVisibility(View.VISIBLE);
                    String nextContent = tagSrc.substring(lineContent.length(), tagSrc.length());
                    tvFrontTwo.setText(nextContent);
                }
            }
        });

 

以上就是直播系统平台搭建,主播个性标签显示在id后面实现的相关代码, 更多内容欢迎关注之后的文章

 

posted @ 2021-10-18 14:12  云豹科技-苏凌霄  阅读(245)  评论(0编辑  收藏  举报