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

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

可是因为突然有一栏要隐藏,导致我的小红点显示很难看,直接跑到字上面去了,于是我决定自定义一个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);
        }
    }


}
 
 
使用方法就省略了
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值