Android 文本视图TextView

本文通过具体实例介绍如何使用TextView实现跑马灯效果及搜索框样式定制。包括XML布局配置与自定义TextView方法,适用于Android应用开发。

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

TextView想必大家都很熟悉了,不管在哪门语言中,文本显示是最为基础的。TextView的基本属性有很多,我就不一一介绍了。这里我们直接通过几个例子来看下TextView的使用。


1.TextView跑马灯效果,这个示例是实现文字滚动效果。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.easygoing.androidtextview.MainActivity">
    
    <com.easygoing.androidtextview.ScrollingTextView
        android:id="@+id/tv_marquee"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:focusable="true"
        android:clickable="true"
        android:focusableInTouchMode="true"
        android:textSize="18sp"
        android:marqueeRepeatLimit="10"
        android:text="静静绽放 吐露芬芳  无论什么境况 静静绽放吐露芬芳"
        />

</LinearLayout>
这里我简单自定义了一个TextView,让TextView一直拥有焦点,从而使跑马灯效果不停止。
package com.easygoing.androidtextview;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

import org.jetbrains.annotations.Nullable;

/**
 * Created by Lenovo on 2017/11/15.
 */

public class ScrollingTextView extends TextView {
    private  Context context;
    public ScrollingTextView(Context context) {
        super(context);
    }

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

    public ScrollingTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        if(focused)
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        if(hasWindowFocus)
        super.onWindowFocusChanged(hasWindowFocus);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}


让我们看下效果图:



其中这几个属性是必须要设置的:

android:singleLine="true" //指定文本单行显示
android:ellipsize="marquee"//指定文本超出范围后的省略方式--start:省略号在开头   middle:省略号在中间 end:省略号在末尾   marquee:跑马灯显示
android:focusable="true"//指定是否获得焦点,跑马灯效果必须为true
android:focusableInTouchMode="true"//指定在触摸时获得焦点,跑马灯效果必须为true


2.TextView搜索框

这里我们简单实现一个搜索框,它有默认的背景色,以及按下的背景色。实例如下图:



代码也很简单:



当然光有布局是不行的,下面我将selector的内容放出来。




另外两个就比较简单了,只是设置下按下时候的背景或文字颜色。





好了,TextView就到这里了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值