[Android开发] 自定义TextView,可以自定义四个方向Drawable的大小、距离文字的距离、设置点击监听等

 我们在用TextView的时候,有时候文本前后都会有一个图片,而这个图片大小在布局中是无法设置的,只有通过 setCompoundDrawables 设置时,给定Drawable的宽高才可以,这无疑是比较麻烦的,如果设置多个方向的图片,又不能单独设置距离文本的距离,另外这个图片是不能够单独点击的,为此我写了一个加强版的 PerfectTextView 

传送门:https://github.com/FlyJingFish/PerfectTextView

支持功能:

1、四个位置的图片在布局中也可单独设置大小

2、四个位置的图片可以单独设置距离文字的距离

3、四个位置的图片可以设置点击、双击、长按事件监听

4、支持设置选中(setSelected)时的文本

5、支持单独为四个位置中的一个设置大小、图片、距离文本距离

6、支持为文本区域设置背景

使用起来也很简单

布局中

<com.flyjingfish.perfecttextviewlib.PerfectTextView
    android:id="@+id/hollowTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:background="@drawable/bg_press"
    android:gravity="center"
    android:textStyle="bold"
    android:padding="10dp"
    android:textColor="@color/press_color"
    android:layout_marginTop="10dp"
    app:perfect_drawableStart_width="40dp"
    app:perfect_drawableStart_height="40dp"
    app:perfect_drawableEnd_width="100dp"
    app:perfect_drawableEnd_height="100dp"
    app:perfect_drawableTop_width="20dp"
    app:perfect_drawableTop_height="20dp"
    app:perfect_drawableBottom_width="50dp"
    app:perfect_drawableBottom_height="50dp"
    android:drawablePadding="40dp"
    app:perfect_drawableStart_padding="20dp"
    app:perfect_drawableEnd_padding="10dp"
    app:perfect_drawableTop_padding="30dp"
    app:perfect_drawableBottom_padding="30dp"
    app:perfect_selected_text="@string/app_name"
    app:perfect_text_background="@drawable/bg_text_press"
    app:perfect_text_background_scope="wrappedText"
    android:drawableTop="@drawable/press_icon"
    android:drawableStart="@drawable/select_icon"
    android:drawableEnd="@drawable/select_icon"
    android:drawableBottom="@drawable/press_icon"
    android:textSize="30sp"/>

以下是监听:

textView.setOnDrawableLeftClickListener(v -> {
    Toast.makeText(this,"setOnDrawableLeftClickListener",Toast.LENGTH_SHORT).show();
    Log.e("PerfectTextView", "setOnDrawableLeftClickListener");
    binding.hollowTextView.setDrawableLeftSelected(true);
});
textView.setOnDrawableLeftDoubleClickListener(v -> {
    Toast.makeText(this,"setOnDrawableLeftDoubleClickListener",Toast.LENGTH_SHORT).show();
    Log.e("PerfectTextView", "setOnDrawableLeftDoubleClickListener");
    binding.hollowTextView.setDrawableLeftSelected(false);
});
textView.setOnDrawableLeftLongClickListener(v -> {
    Toast.makeText(this,"setOnDrawableLeftLongClickListener",Toast.LENGTH_SHORT).show();
    Log.e("PerfectTextView", "setOnDrawableLeftLongClickListener");

    return true;
});

 设置图片

textView.setDrawableLeft(R.mipmap.ic_launcher_round);//设置图片
textView.setDrawableStartPadding(100);//设置距离文字距离
textView.setDrawableStartWidthHeight(100,100);//设置宽高
textView.setSelectedText("选中时显示的文本");//设置选中时显示的文本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android自定义TextView中显示全部内容,可以使用以下两种方法: 1. 使用setEllipsize()方法 通过设置setEllipsize()方法,可以在TextView的末尾添加省略号,从而指示文本被截断。你可以使用以下代码来实现: ``` yourTextView.setEllipsize(TextUtils.TruncateAt.END); yourTextView.setSingleLine(true); ``` 上述代码将设置TextView只显示一行并在末尾添加省略号。 2. 自定义TextView 你可以从TextView类继承一个新类,并覆盖onMeasure()方法以测量控件的高度和宽度。 你可以使用以下代码实现: ``` public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取TextView的内容 CharSequence text = getText(); if (text != null) { //测量TextView的高度 int width = getMeasuredWidth(); int height = getMeasuredHeight(); int lineCount = getLineCount(); int lineHeight = getLineHeight(); int totalHeight = lineCount * lineHeight; if (totalHeight > height) { setMeasuredDimension(width, totalHeight); } } } } ``` 上述代码将测量TextView的高度,如果文本的高度超出了TextView的高度,则调整TextView的高度以适应文本。然后你可以使用此自定义TextView来显示你的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值