不受父容器大小约束的TextView

序言

为了实现以下效果,特意开发了一个自定义控件。主要是红色的点赞数和评论数。
在这里插入图片描述

问题分析

在这里插入图片描述

自定义控件

该控件主要是在于忽略的父容器的大小限制,这样可以展示出全部内容。注意父容器的属性中需要下列配置。

package com.trs.myrb.view.count;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zgh.trsbadge.TextUtils;

/**
 * <pre>
 * Created by zhuguohui
 * Date: 2023/12/22
 * Time: 10:51
 * Desc:这是一种没有被父类的尺寸约束大小的TextView,内容有多少就显示多少。
 * 主要目的是可以将显示的内容超过父容器的大小。必须将配置父容器的clipChildren为false。才能看到效果。
 * 目前用在网页底部工具条显示评论数,点赞数等。父控件的大小是均分的,而显示数量有可能超过父控件。
 * </pre>
 */
public class NoSizeTextView extends androidx.appcompat.widget.AppCompatTextView {

    private int measuredHeight;
    private int measuredWidth;

    public NoSizeTextView(@NonNull Context context) {
        super(context);
    }

    public NoSizeTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        super.onMeasure(spec, spec);
        measuredHeight = getMeasuredHeight();
        measuredWidth = getMeasuredWidth();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, left+measuredWidth, top+measuredHeight);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {

        super.dispatchDraw(canvas);
    }

    @Override
    public void draw(Canvas canvas) {
        CharSequence text = getText();
        //这部分是为了在数据量为0的时候自动隐藏,可以根据具体业务场景进行保留或删除
        if(TextUtils.isEmpty(text)||"0".contentEquals(text)){
            return;
        }
        super.draw(canvas);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
    }
}

参考布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/status_layout"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/normal_background_color">

    <RelativeLayout
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:layout_width="match_parent"
            android:layout_height="@dimen/TRSDividerSize"
            android:background="@color/divider_color" />

        <TextView
            android:id="@+id/tv_comment"
            android:layout_width="187dp"
            android:layout_height="33dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:background="@drawable/bg_common_text"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:text="我来说两句..."
            android:textColor="@color/second_title_color"
            android:textSize="@dimen/TRSSecondTitleSize" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toEndOf="@id/tv_comment"
            android:clipChildren="false"
            android:gravity="center"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toEndOf="@id/tv_comment"
                android:clipChildren="false"
                android:gravity="center"
                android:orientation="horizontal">

                <RelativeLayout
                    android:id="@+id/comment_layout"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:clipChildren="false"
                    android:visibility="visible">

                    <ImageView
                        android:id="@+id/iv_comment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:scaleType="center"
                        android:src="@drawable/ic_comment" />

                    <com.trs.myrb.view.count.NoSizeTextView
                        android:id="@+id/tv_comment_number"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_above="@id/iv_comment"
                        android:layout_marginStart="-8dp"
                        android:layout_marginBottom="-5dp"
                        android:layout_toEndOf="@id/iv_comment"
                        android:background="@drawable/shape_red_commet"
                        android:paddingStart="3dp"
                        android:paddingEnd="3dp"
                        android:textColor="@color/white"
                        android:textSize="9sp"
                        tools:text="1000" />
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/like_layout"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:clipChildren="false"
                    android:visibility="visible">

                    <ImageView
                        android:id="@+id/iv_like"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:layout_weight="1"
                        android:scaleType="center"
                        android:src="@drawable/web_bottom_view_like" />


                    <com.trs.myrb.view.count.NoSizeTextView
                        android:id="@+id/tv_like_number"
                        android:layout_width="200dp"
                        android:layout_height="wrap_content"
                        android:layout_above="@id/iv_like"
                        android:layout_marginStart="-8dp"
                        android:layout_marginBottom="-5dp"
                        android:layout_toEndOf="@id/iv_like"
                        android:background="@drawable/shape_red_commet"
                        android:paddingStart="3dp"
                        android:paddingEnd="3dp"
                        android:textColor="@color/white"
                        android:textSize="9sp"
                        tools:text="10000" />
                </RelativeLayout>


                <ImageView
                    android:id="@+id/iv_collected"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:scaleType="center"
                    android:src="@drawable/bg_collected" />

                <ImageView
                    android:id="@+id/iv_share"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:scaleType="center"
                    android:src="@drawable/ic_web_share" />
            </LinearLayout>

            <TextView
                android:id="@+id/tv_comment_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layout_loading"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/normal_background_color"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:orientation="horizontal">

            <ProgressBar
                android:id="@+id/progressbar"
                android:layout_width="30dp"
                android:layout_height="30dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/str_loading" />
        </LinearLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/layout_error"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_item_common"
        android:visibility="gone">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/str_error" />

    </RelativeLayout>
</FrameLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/264395.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

[JS设计模式]Flyweight Pattern

Flyweight pattern 享元模式是一种结构化的设计模式&#xff0c;主要用于产生大量类似对象而内存又有限的场景。享元模式能节省内存。 假设一个国际化特大城市SZ&#xff1b;它有5个区&#xff0c;分别为nanshan、futian、luohu、baoan、longgang&#xff1b;每个区都有多个图…

Java 基础学习(十三)集合框架、List集合

1 集合框架 1.1 Collection 1.1.1 集合框架概述 Java 集合框架是一组实现了常见数据结构&#xff08;如列表、树集和哈希表等&#xff09;的类和接口&#xff0c;用于存储一组数据。 开发者在使用Java的集合类时&#xff0c;不必考虑数据结构和算法的具体实现细节&#xff…

使用selenium webdriver和mitmproxy代理模拟用户点击抓包(抓华为应用商城app数据)

文章目录 安装PythonMacWindows 安装程序需要的依赖安装chorm驱动编写代码自动化程序开始抓包 问题处理 本文简单记录一下使用selenium webdriver和mitmproxy代理模拟用户点击抓包的过程。 用于模拟真实的用户访问网站&#xff0c;达到抓包的目的。 作者水平有限&#xff0c;可…

深度解析自动化测试流程(纯干货)

今天就通过这篇文章给大家深度解析一下自动化测试的流程。 自动化测试的流程和功能测试其实挺相似的&#xff0c;整个流程也是按照需求分析及测试计划阶段、测试设计阶段、测试执行和测试总结阶段&#xff0c;总结下来就是下面一张图&#xff0c;ppt中纯手绘&#xff0c;效果不…

天猫数据分析(软件工具)-2023年11月天猫保健品行业分析报告:市场需求扩容,年轻人是主流群体

近年来&#xff0c;随着健康经济、颜值经济的兴起&#xff0c;越来越多的年轻人加入养生大军&#xff0c;成为保健食品市场上的一股新力量&#xff0c;带动市场扩容。根据鲸参谋电商数据分析平台的相关数据显示&#xff0c;今年11月份&#xff0c;天猫平台上保健食品的销量为24…

怎么提取视频中的背景音乐?

当我们在刷视频的时候&#xff0c;有时候听到一个背景音乐很好听&#xff0c;但是又不知道歌名&#xff0c;比如英语歌&#xff0c;这个时候我们很难找到这首歌&#xff0c;相信有很多朋友会遇到这样的问题&#xff0c;不知道怎么弄&#xff0c;下面小编给大家推荐一些方法帮助…

主流数据库体系结构

MySQL 我们通常所说的 MySQL 数据库服务器由一个实例&#xff08;instance&#xff09;以及一个数据库&#xff08;database&#xff09;组成。实例包括一组后台进程/线程和许多内存结构&#xff0c;用于管理数据库&#xff1b;数据库由一组磁盘文件组成&#xff0c;用于存储数…

【数论】欧拉函数

前置知识&#xff1a;分解质因数 一个数可以被分解为质因数乘积 n &#xff0c;其中的pi都是质因数 欧拉函数介绍 朴素法求欧拉函数 思路&#xff1a;边分解质因数边算欧拉函数 void get_primes() {int n; cin >> n;int ans 0;int res n;for (int i 2; i < n /…

docker部署个人网站项目记录(前后端分离)

背景 项目是前后端分离&#xff0c;前端有三部分&#xff0c;分别是 个人网站&#xff08;blog&#xff09;网站后台管理系统&#xff08;admin&#xff09;数据大屏&#xff08;datascreen&#xff09; 后端是基于nodejs写的后台服务 后台接口服务&#xff08;todo-nodejs…

表情识别-情感分析-人脸识别(代码+教程)

表情识别是计算机视觉领域中的一个重要研究方向&#xff0c;它的目标是通过分析人脸表情来判断人的情绪状态。表情识别在很多领域都有广泛的应用&#xff0c;如情感分析、人机交互、智能驾驶等。本文将从以下几个方面来阐述表情识别的相关内容。 一、表情识别的基本原理 表情识…

面试 Java 算法高频题五问五答第二期

面试 Java 算法高频题五问五答第二期 作者&#xff1a;程序员小白条&#xff0c;个人博客 相信看了本文后&#xff0c;对你的面试是有一定帮助的&#xff01; ⭐点赞⭐收藏⭐不迷路&#xff01;⭐ 寻找峰值: 主要思想&#xff1a;二分查找&#xff0c;利用get函数&#xff0…

操作系统 day15(信号量)

信号量机制 之前学习了这些解决进程互斥的方案 *但它们都无法实现“让权等待”&#xff0c;于是Dijkstra提出了一种卓有成效的实现进程互斥、同步的方法----信号量机制 总结&#xff1a;一个信号量对应一种资源。信号量的值这种资源的剩余数量&#xff08;信号量的值如果小于…

Python实现广义最小二乘法线性回归模型(GLS算法)项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档视频讲解&#xff09;&#xff0c;如需数据代码文档视频讲解可以直接到文章最后获取。 1.项目背景 广义最小二乘法&#xff08;Generalized Least Squares&#xff09;是一种回归分析方法&#xff0c;适…

NLP论文阅读记录 - | 文本生成的动量校准

文章目录 前言0、论文摘要一、Introduction1.1目标问题1.2相关的尝试1.3本文贡献 二.相关工作三.本文方法3.1 神经文本生成3.2 动量校准 四 实验效果4.1数据集4.2 对比模型4.3实施细节4.4评估指标4.5 实验结果4.6 消融实验 五 总结 前言 Momentum Calibration for Text Generat…

MyBatis 关联查询

目录 一、一对一查询&#xff08;sqlMapper配置文件&#xff09; 1、需求&#xff1a; 2、创建account和user实体类 3、创建AccountMapper 接口 4、创建并配置AccountMapper.xml 5、测试 二、一对多查询&#xff08;sqlMapper配置文件&#xff09; 1、需求&#xff1a;…

2024年危险化学品生产单位安全生产管理人员证模拟考试题库及危险化学品生产单位安全生产管理人员理论考试试题

题库来源&#xff1a;安全生产模拟考试一点通公众号小程序 2024年危险化学品生产单位安全生产管理人员证模拟考试题库及危险化学品生产单位安全生产管理人员理论考试试题是由安全生产模拟考试一点通提供&#xff0c;危险化学品生产单位安全生产管理人员证模拟考试题库是根据危…

如何通过蓝牙串口启动智能物联网?

1、低功耗蓝牙(BLE)介绍 BLE 技术是一种低成本、短距离、可互操作的鲁棒性无线技术&#xff0c;工作在免许可的 2,4 GHZ 工业、科学、医学(Industrial Scientific Medical&#xff0c;ISM)频段。BLE在设计之初便被定位为一种超低功耗(Ultra Low Power&#xff0c;ULP)无线技术&…

7ADC模数转换器

一.模数转换原理 ADC模拟-数字转换器可以将引脚上连续变化的模拟电压转换成内存中存储的数字变量&#xff0c;建立模拟电路到数字电路的桥梁。另外一种是DAC既是与前面相反&#xff0c;如PWM波&#xff0c;由于PWM电路简单且没有额外的功率损耗&#xff0c;更适用于惯性系统的…

Protobuf 编码规则及c++使用详解

Protobuf 编码规则及c使用详解 Protobuf 介绍 Protocol Buffers (a.k.a., protobuf) are Google’s language-neutral, platform-neutral, extensible mechanism for serializing structured data Protocol Buffers&#xff08;简称为protobuf&#xff09;是谷歌的语言无关、…

云服务器2核4g能干什么?

​  对于许多个人和企业来说&#xff0c;云服务器的硬件配置是至关重要的。其中&#xff0c;常见的有2核4G配置。 谈到2核4g配置&#xff0c;它是指云服务器拥有2个CPU核心和4GB的内存。2核指的是处理器(CPU)的核心数量&#xff0c;而4G则是指内存的大小。这个配置通常对于中…
最新文章