底部动态添加不同颜色标签

2020-03-10  本文已影响0人  勤劳的蚂蚁
image.png

主布局

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
   
    <data></data>
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_96dp"
        android:background="@color/color_FFFFFF">

        <ImageView
            android:id="@+id/search_imgView"
            android:layout_width="@dimen/dimen_114dp"
            android:layout_height="@dimen/demen_69dp"
            android:layout_gravity="center"
            android:layout_marginLeft="@dimen/margin12"
            android:scaleType="fitXY"
            android:src="@mipmap/default_iv"
            bind:layout_constraintBottom_toBottomOf="parent"
            bind:layout_constraintLeft_toLeftOf="parent"
            bind:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/search_view"
            android:layout_width="0dp"
            android:layout_height="0.5dp"
            android:layout_marginLeft="@dimen/margin12"
            android:layout_marginRight="@dimen/margin12"
            android:background="#eeeeee"
            android:visibility="visible"
            bind:layout_constraintBottom_toBottomOf="parent"
            bind:layout_constraintLeft_toLeftOf="parent"
            bind:layout_constraintRight_toRightOf="parent" />



        <TextView
            android:id="@+id/search_tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:includeFontPadding="false"
            android:maxLines="1"
            android:singleLine="true"
            android:textColor="@color/color333333"
            android:textSize="@dimen/text12"
            bind:layout_constrainedWidth="true"
            bind:layout_constraintHorizontal_bias="0"
            bind:layout_constraintHorizontal_chainStyle="packed"
            bind:layout_constraintLeft_toRightOf="@+id/search_imgView"
            bind:layout_constraintRight_toRightOf="parent"
            bind:layout_constraintTop_toTopOf="@+id/search_imgView"
            android:layout_marginLeft="@dimen/margin12"
            android:layout_marginRight="@dimen/margin12"
            android:layout_marginTop="@dimen/margin5"
            tools:text="标题标题题题题标题" />
        <TextView
            android:id="@+id/search_tv_content"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin10"
            android:ellipsize="end"
            android:maxLines="1"
            android:textColor="@color/color666666"
            android:includeFontPadding="false"
            android:textSize="@dimen/text11"
            bind:layout_constraintLeft_toLeftOf="@+id/search_tv_title"
            bind:layout_constraintTop_toBottomOf="@+id/search_tv_title"
            bind:layout_constraintRight_toRightOf="parent"
            android:layout_marginRight="@dimen/margin12"
            tools:text="内容" />
        <!--    眼睛-->
        <TextView
            android:id="@+id/search_tv_eye"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin6"
            android:drawableLeft="@mipmap/eye"
            android:ellipsize="end"
            android:maxLines="1"
            android:drawablePadding="@dimen/margin5"
            android:textColor="@color/color666666"
            android:textSize="@dimen/text11"
            android:visibility="gone"
            android:layout_marginRight="@dimen/margin12"
            bind:layout_constraintRight_toRightOf="parent"
            bind:layout_constraintTop_toBottomOf="@+id/search_tv_content"
            tools:text="内容" />

            <LinearLayout
                android:id="@+id/search_planbook_ll_label"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                bind:layout_constrainedWidth="true"
                android:layout_marginLeft="@dimen/margin12"
                android:layout_marginRight="@dimen/margin12"
                bind:layout_constraintLeft_toRightOf="@+id/search_imgView"
                bind:layout_constraintRight_toLeftOf="@+id/search_tv_eye"
                android:visibility="visible"
                bind:layout_constraintTop_toBottomOf="@+id/search_tv_content"
                android:layout_marginTop="@dimen/margin6">
            </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</layout>


标签布局

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    >

    <TextView
        android:id="@+id/tv_inner_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/margin3"
        android:paddingRight="@dimen/margin3"
        android:textSize="@dimen/text10"
        android:layout_marginRight="@dimen/margin10"
        android:background="@drawable/shape_library_label"/>
</FrameLayout>

shape_library_label 样式为:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!--圆角-->
    <corners android:radius="@dimen/dimen_2dp"/>
</shape>

private View setLabel(String content, String color, boolean isTopHotChoice) { //设置标签
        View label_item = View.inflate (mContext, R.layout.item_search_innerview, null);
        TextView label =  label_item.findViewById (R.id.tv_inner_label);
        try {
            label.setText (content);
          //  if (isTopHotChoice) {
         //       label.setTextColor (Color.parseColor ("#ffffff"));
         //       GradientDrawable p = (GradientDrawable) label.getBackground ();
         //       p.setColor (Color.parseColor (color));
        //        p.setStroke (DensityUtil.dp2px (mContext, 1), Color.parseColor (color));
        //    } else {
                label.setTextColor (Color.parseColor (color));
                GradientDrawable p = (GradientDrawable) label.getBackground ();
                p.setColor (Color.parseColor ("#ffffff"));
                p.setStroke (DensityUtil.dp2px (mContext, 1), Color.parseColor (color));
         //   }
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return label_item;
    }

设置其他属性:

((ViewHolder) holder).getBinding ().searchLlLabel.removeAllViews ();
            if ("1".equals (knowledgeBean.getTopFlag ())) {
                View textTop = setKnowLedgeLabel ("置顶", "#FDB302", true);
                ((ViewHolder) holder).getBinding ().searchLlLabel.addView (textTop);
            }
            if ("1".equals (Bean.getChoiceFlag ())) {
                View textChoice = setLabel ("精华", "#30CA07", true);
                ((ViewHolder) holder).getBinding ().searchLlLabel.addView (textChoice);
            }
            if ("1".equals (Bean.getHotFlag ())) {
                View textHot = setLabel ("热点", "#FF5722", true);
                ((KViewHolder) holder).getBinding ().searchLlLabel.addView (textHot);
            }
            for (int i = 0; i < Bean.getLabels ().size (); i++) {
                if (((ViewHolder) holder).getBinding ().searchLlLabel.getChildCount () < 5) {
                    View label = setLabel (Bean.getLabels ().get (i).getName (), Bean.getLabels ().get (i).getColor (), false);
                    ((ViewHolder) holder).getBinding ().searchLlLabel.addView (label);
                }
            }
上一篇下一篇

猜你喜欢

热点阅读