美文网首页
textview 设置不同的背景颜色背景

textview 设置不同的背景颜色背景

作者: 勤劳的蚂蚁 | 来源:发表于2020-03-03 15:27 被阅读0次

    在做需求功能的时候 ,经常会遇到给textview 设置不同的背景颜色,有许多的方法

    1.  获取不同的图片背景,设置不同的颜色;
    2. 通过xml 设置不同的shape ,设置不同的背景
    tv_cus_rank.setBackgroundResource(R.drawable.shape_biaoji);
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="@color/textb5ee9d" />
        <corners android:radius="1dp" />
        <padding
            android:left="5dp"
            android:right="5dp"
         />
    </shape>
    3. 考虑 如果shape 样式 相同,只是颜色不同,动态改变 shape 颜色  不需要写多个 shape
     TextView tv_inner_label = label_item.findViewById (R.id.tv_inner_label);
            try {
                tv_inner_label.setText (content);
                GradientDrawable p = (GradientDrawable) tv_inner_label.getBackground ();
                p.setColor (Color.parseColor (color));
            } catch (Exception e) {
                e.printStackTrace ();
                MyLog.wtf (TAG, "颜色的值有问题");
            }
    
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="#fff" />
        <!-- 圆角 -->
        <corners android:radius="2dp" />
    </shape>
    

    相关文章

      网友评论

          本文标题:textview 设置不同的背景颜色背景

          本文链接:https://www.haomeiwen.com/subject/uochlhtx.html