安卓自定义界面——textview显示带小红点

时间:2024-04-01 12:44:29

        这个自定义view是从我原有的项目中提取出来的。

安卓自定义界面——textview显示带小红点

可是因为突然有一栏要隐藏,导致我的小红点显示很难看,直接跑到字上面去了,于是我决定自定义一个view,来满足需求


代码如下:

textview_withpoint.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <TextView
       android:id="@+id/tv_content"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:paddingBottom="@dimen/space_10"
       android:paddingTop="@dimen/space_10"
       />
    <TextView
        android:id="@+id/tv_notice"
        android:layout_width="@dimen/space_08"
        android:layout_height="@dimen/space_08"
        android:background="@mipmap/sy_new_xx"
        android:layout_alignParentRight="true"
        android:layout_marginRight="@dimen/space_05"
        android:layout_marginTop="@dimen/space_05"
        android:visibility="invisible"
        />
</RelativeLayout>




import android.content.Context;
import android.content.res.ColorStateList;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;



/**
 * Created by dawn on 2017/11/21.
 *  QQ:272398235
 * 功能动态实现textview边上的小红点
 */

public class TextViewWithPoint extends RelativeLayout{
    private TextView tv_content,tv_notice;
    private Context context;
    public TextViewWithPoint(Context context) {
        super(context);
        this.context=context;
        setupView();
    }

    public TextViewWithPoint(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        setupView();
    }


    private void setupView() {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.textview_withpoint, this);
        tv_content= (TextView) findViewById(R.id.tv_content);
        tv_notice= (TextView) findViewById(R.id.tv_notice);
    }

    public void setText(String msg){
        tv_content.setText(msg);
    }

    public void setTextColor(@ColorInt int color) {
        tv_content.setTextColor(color);
    }

    /**
     * @param bol  true==显示红点
     *             false==不显示红点
     */
    public void setRedPoint(boolean bol){
        if(bol){
            tv_notice.setVisibility(VISIBLE);
        }else{
            tv_notice.setVisibility(INVISIBLE);
        }
    }


}


使用方法就省略了