Android自定义控件之extends textview

本文介绍如何通过自定义状态的方式,在TextView中实现多状态显示效果。包括自定义视图、状态下的drawable资源及布局使用,最终演示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android自定义控件之extends textview

上效果图:


实现:

这个效果的实现有好多个方式,这里使用的是一个自定义状态的方式,咱们分三步走,代码如下:

1)自定义view,继承 TextView。当然对状态进行了自定义。

public class PseudoAwareTextView extends TextView {
    
    private static final int[] STATE_ONLY_ONE = new int[] {
        android.R.attr.state_first,
        android.R.attr.state_last,
    };
     
    private static final int[] STATE_FIRST = new int[] {
        android.R.attr.state_first
    };
     
    private static final int[] STATE_LAST = new int[] {
        android.R.attr.state_last
    };
 
    public PseudoAwareTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
     
    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        ViewGroup parent = (ViewGroup) getParent();
        if (parent == null) {
            return super.onCreateDrawableState(extraSpace);
        }
         
        final int size = parent.getChildCount();
        final boolean isFirst = (parent.getChildAt(0) == this);
        final boolean isLast = (parent.getChildAt(size-1) == this);
         
        int[] states = super.onCreateDrawableState(extraSpace + 2);
        if (isFirst && isLast) {
            mergeDrawableStates(states, STATE_ONLY_ONE);
        } else if (isFirst) {
            mergeDrawableStates(states, STATE_FIRST);
        } else if (isLast) {
            mergeDrawableStates(states, STATE_LAST);
        }
         
        return states;
    }
 
}

2)自定义不同状态下相对应的drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- First and last -->
    <item android:state_first="true" android:state_last="true"><shape>
            <solid android:color="#63abed" />

            <corners android:radius="10dp" />
        </shape></item>

    <!-- First -->
    <item android:state_first="true"><shape>
            <solid android:color="#63abed" />

            <corners android:bottomLeftRadius="10dp" android:topLeftRadius="10dp" />
        </shape></item>

    <!-- Last -->
    <item android:state_last="true"><shape>
            <solid android:color="#63abed" />

            <corners android:bottomRightRadius="10dp" android:topRightRadius="10dp" />
        </shape></item>

    <!-- Default -->
    <item><shape>
            <solid android:color="#63abed" />
        </shape></item>

</selector>

3)当然是在布局中使用,注意此时这几个view的父容器必须只是包含这几个自定义控件,不然效果会出错的。

<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.androidtest.MainActivity" >
    
    <com.example.androidtest.PseudoAwareTextView
        android:id="@+id/pseudoAwareTextView1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/sss"
        android:text="PseudoAwareTextView1" />

    <com.example.androidtest.PseudoAwareTextView
        android:id="@+id/pseudoAwareTextView2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/sss"
        android:text="PseudoAwareTextView2" />

    <com.example.androidtest.PseudoAwareTextView
        android:id="@+id/pseudoAwareTextView3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/sss"
        android:text="PseudoAwareTextView3" />

</LinearLayout>

到此为止,试试效果怎么样。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值